我遇到了一个小问题。我创建了一个程序,用户必须选择 5 个选项(1.输入数字 2. 显示最小 3. 显示最大 4. 显示所有数字 5 退出。)所有选项都有效。问题是当用户选择选项时,如果按下任何字母都会给我错误。我希望如果用户按下任何字母或任何符号成为警告消息,例如“输入了未知的选项值”,然后再试一次。我想是关于转换或类似的东西,但我找不到问题出在哪里。这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignmen2014_15
{
class Program
{
const int MAXNUMBERS = 3;
static void Main(string[] args)
{
int[] theNumbers = new int[MAXNUMBERS];
int chosenOption = 0;
bool quit = false;
InitialiseNumbers(theNumbers);
while (quit == false)
{
DisplayHeader();
DisplayMenu();
chosenOption = ReadNumber("Please choose an option: ");
quit = ProcessMenu(chosenOption, theNumbers);
Console.Clear();
}
}
static void InitialiseNumbers(int[] numbers)
{
for (int index = 0; index < MAXNUMBERS; index++)
{
numbers[index] = 0;
}
}
static void DisplayHeader()
{
WriteText("*******************************************************************************", 0, 0); // Top left hand corner of screen is x = 0, y = 0;
WriteText("* This application is designed to allow you to choose numbers *", 0, 1); // Next line down is x = 0, y = 1, etc WriteText("* and finds the biggest and the smallest value *", 0, 2);
WriteText("*******************************************************************************", 0, 3);
}
static void DisplayMenu()
{
WriteText("Select an option", 20, 8); // Display menu at at x = 20, y = 8
WriteText("1. Enter the numbers", 20, 9);
WriteText("2. Find the smallest", 20, 10);
WriteText("3. Find the largest", 20, 11);
WriteText("4. Display all numbers", 20, 12);
WriteText("5. Quit", 20, 13);
}
侃侃尔雅
相关分类