确保用户输入不是字符串

时间:2014-10-19 15:52:00

标签: c#

我无法检查用户输入是否为整数/实数且在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
                    }

1 个答案:

答案 0 :(得分:0)

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

首先移动while循环中的前两行并删除needInput变量

// 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
 }