编程用户输入文本而不是整数值

时间:2015-12-08 14:07:10

标签: c# int boolean

只是浏览我的代码并尝试在测试时尝试解决问题当用户在菜单中以文本形式输入选项时如何让程序停止退出控制台,例如他们想要选择选项" 1"但决定输入单词" one"代替。

由于 丹

int numNumbersReqd = 5;
            int chosenOption = 0;
            bool quit = false;

            Console.WriteLine("*****************************************************************************");
            Console.WriteLine("* This application is designed to allow you to select the number of numbers *");
            Console.WriteLine("* you wish to enter, and sum the numbers that you provide.                  *");
            Console.WriteLine("*****************************************************************************");

            while (quit == false)
            {
                DisplayMenu();
                chosenOption = ReadNumber("Please choose an option:");
                quit = ProcessMenu(chosenOption, ref numNumbersReqd);

            }

        }

        static void DisplayMenu()
        {
            Console.WriteLine("Select an option");
            Console.WriteLine("1. Add numbers");
            Console.WriteLine("2. Change amount of numbers to be entered");
            Console.WriteLine("3. Quit");
        }

        static int ReadNumber(string prompt)
        {
            string text;
            int number;


            Console.Write(prompt);
            text = Console.ReadLine();
            number = int.Parse(text);

            return number;
        }

        static bool ProcessMenu(int option, ref int numNumbers)
        {
            bool quit = false;

            switch (option)
            {
                case 1:
                    int total;
                    total = GetTotal(numNumbers);
                    DisplayResult(total); // Programme was initally set to display the option that the user had entered from "displayMenu", by changing the variabel from option to total it will now display the answer of the numbers entrered by the user
                    break;
                case 2:
                    numNumbers = ReadNumber("Please enter the number of numbers to be entered:");
                    break;
                case 3:
                    quit = IsQuitting();
                    break;
                default:
                    Console.WriteLine("Unknown option value entered");
                    break;
            }

            return quit;
        }

        static int GetTotal(int numbersReqd)
        {
            int total;
            int index;

            total = 0;

            for (index = 0; index < numbersReqd - 1; index++)
            {
                total = total + ReadNumber("Please Enter Number:");
            }

            return total;
        }

        static void DisplayResult(int total)

        {
            Console.WriteLine("###############################################################################");
            Console.WriteLine("###############################################################################");
            Console.WriteLine("##################                     " + total.ToString() + "                     ##################");
            Console.WriteLine("###############################################################################");
            Console.WriteLine("###############################################################################");
        }

        static bool IsQuitting()
        {
            string response;
            bool quit = false;

            Console.Write("Do you really wish to quit?");

            response = Console.ReadLine();
            response = response.ToLower();

            if (response == "yes") // Problem fixed with quit option, "YES" used before was specifying uppercase only inputs which are highly unlikely to be entered by the user which caused the programme to loop back to the start menu,
            {
                Console.WriteLine("Quiting");
                quit = true;
            }

            return quit;

2 个答案:

答案 0 :(得分:1)

使用此:

int number;

while (!int.TryParse(Console.ReadLine(), out number)) // make sure that the user enters a valid number
    Console.WriteLine("You did not enter a valid number");
// here number will have the value entered by the user.
如果第一个参数是有效整数,

TryParse将返回truefalse,并使用out关键字将该值分配给第二个参数。

&#34;定期&#34; Parse将字符串转换为整数,或者如果它不是有效整数则抛出异常。

编辑:cFrozenDeath建议对我的帖子进行良好的编辑,将TryParse包围在while循环中。实际上,它正在做的是,它将从用户UNTIL输入有效数字时获得输入。一般认为这是一个好主意;如果他们在事故中输入错误的东西,你很可能不想立即退出/结束该计划。

但是,while条件中没有逻辑检查值是否为&#34;有效&#34;从要求的角度来看。您的菜单有3个选项,但是如果它们输入100,则它们输入了数学上有效的整数,但不是您应用程序的有效整数。因此,您可能还需要添加一个或两个条件来检查number是否在可接受的范围内。

答案 1 :(得分:-2)

您可以使用int32.TryParse()而不是int.Parse()。 解析失败时该值为0,然后您可以采取相应的行动。