确保用户输入不是字符串

本文关键字:字符串 输入 用户 确保 | 更新日期: 2023-09-27 18:27:14

我在检查用户输入是否为整数/实数并且在12-36范围内时遇到问题。如果输入不是数字,我似乎无法让程序输出错误消息。我需要程序使用Tryparse检查输入是否是一个数字,但如果不是,则会出现错误消息。错误消息不起作用,相反,当我试图运行它时,程序崩溃了。

Console.Write("Please enter the diameter of your pizza: "); // get user to input pizzaDiameter
        Double.TryParse(Console.ReadLine(), out pizzaDiameter); // read the users keyboard
        //and convert input to real number to hold as the pizzaDiameter variable

        /**********************************************************************************
          *                                                                               *
          *                         PROCESSING                                            *
          *                                                                               *
          * *******************************************************************************/
        while (pizzaDiameter != QUIT_PROGRAM && needInput) // Begin while loop
        {

            // determine if diameter is within 12 to 36
            // if does not meet requirements show error message and have user enter in new diameter
            //if (Double.TryParse(Console.ReadLine(), out pizzaDiameter))
            //{
                //Console.WriteLine("'nENTRY ERROR'n'nPizza diameter must be a whole or real number.'n'nPlease try again");
            //}
            //else
            //{
                //needInput = false;
            //}
                if (pizzaDiameter < MINIMUM_DIAMETER || pizzaDiameter > MAXIMUM_DIAMETER)
                {
                    //error message detailing why the program failed to calculate
                    Console.WriteLine("'nENTRY RANGE ERROR'n'nPizza must have a diameter in the range of 12” to 36” inclusive!'n'nPlease try again");
                }
                else
                {    //program is satisfied with the inputted amount and will now move on to the range checking
                    needInput = false;

                    //Determines the number of slices based on user inputted diameter
                    if (pizzaDiameter <= DIAMETER_SMALL)//checks if pizzaDiameter is <=small
                    {
                        pizzaSlices = (SLICES_MINIMUM);//sets pizzaSlices to the minimum of 8
                    }
                    else if (pizzaDiameter <= DIAMETER_MEDIUM)//diameter range for <24
                    {
                        pizzaSlices = (SLICES_MEDIUM);// sets number of slices to 12
                    }
                    else if (pizzaDiameter <= DIAMETER_LARGE)//diameter range for <30
                    {
                        pizzaSlices = (SLICES_HIGH);// sets number of slices to 16
                    }
                    else
                    {
                        pizzaSlices = (SLICES_MAXIMUM);//sets maximum slices because diameter is >30
                    }

确保用户输入不是字符串

代码中存在逻辑错误和琐碎错误。

从移动while循环内的前两行开始,并删除所需的输入变量

// Just to enter the loop the first time....
double pizzaDiameter = 1.0d;
const double QUIT_PROGRAM = 0.0d;
while (pizzaDiameter != QUIT_PROGRAM) 
{
     Console.Write("Please enter the diameter of your pizza: (0 to exit) "); 
     // Add the NOT operator in front of the TryParse call
     if(!Double.TryParse(Console.ReadLine(), out pizzaDiameter))
     {
        ... false returned by TryParse
        ... error message....
     }
     else if( .... check for diameter min/max .....)
     {
        ... diameter not valid error message
     }
     else
     {
        .... start calculus....
     }
     // end while here... 
     // if input is incorrect the loop restart until diameter = 0
     // So, no need to ask again the pizza diameter in this point
 }