不允许不同的选项

我遇到了一个小问题。我创建了一个程序,用户必须选择 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);

        }


DIEA
浏览 89回答 1
1回答

侃侃尔雅

使用Int32.TryParse(String, Int32)尝试将字符串解析为整数并返回表示解析是否成功的布尔值的方法。在您的情况下,如果解析失败,您可以从您的ReadNumber方法中返回 -1,该方法将调用default:您的部分 switch case,并且将显示一条错误消息。否则,如果解析成功,您可以简单地返回解析的数字,这将是您想要的数字之一,或者它会调用default:action。以下是Microsoft 文档中的示例String[] values = { null, "160519", "9432.0", "16,667",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&nbsp; &nbsp;-322&nbsp; &nbsp;", "+4302", "(100);", "01FA" };foreach (var value in values)&nbsp;{&nbsp; &nbsp;int number;&nbsp; &nbsp;bool success = Int32.TryParse(value, out number);&nbsp; &nbsp;if (success)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; Console.WriteLine("Converted '{0}' to {1}.", value, number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;}&nbsp; &nbsp;else&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; Console.WriteLine("Attempted conversion of '{0}' failed.",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value ?? "<null>");&nbsp; &nbsp;}}在您的具体示例中,您需要修改您的ReadNumber(string prompt)方法:static int ReadNumber(string prompt){&nbsp; &nbsp; string text;&nbsp; &nbsp; int number;&nbsp; &nbsp; WriteText(prompt, 20, 14);&nbsp; &nbsp; text = Console.ReadLine();&nbsp; &nbsp; bool is_parsing_successful = Int32.TryParse(text, out number);&nbsp; &nbsp; ClearText(20, 14, prompt.Length + text.Length); // Clear the text at this line&nbsp; &nbsp; if(is_parsing_successful){&nbsp; &nbsp; &nbsp; &nbsp; return number;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP