简体   繁体   English

C#我如何要求用户在输入无效输入时按任意键退出

[英]c# how do i ask the user to press any keys to exit when they enter an invalid input

I don't know how to ask user to press any keys to exit when they enter an invalid input. 我不知道如何要求用户在输入无效输入时按任意键退出。

Whenever i try to put Console.ReadKey (), the program just keeps on going. 每当我尝试放置Console.ReadKey ()时,该程序就会继续运行。 I don't know how to make it stop when user did not put a valid input. 当用户未输入有效输入时,我不知道如何使其停止。

Here's a screenshot 这是截图 在此处输入图片说明

Here's the code 这是代码

 /* Variable */

        int height = 0;                // stores user's height input
        int weight = 0;               // stores user's weight input
        const int factorBMI = 703;        // stores user's constant bmi which is 703
        double bmi = 0.0;                // stores user's calculated bmi
        /* User's Input */
        Console.WriteLine("************************************************************************************************************************");
        Console.WriteLine(" Please enter the person's height in inches:");                                         // Ask user to enter their height in inches
        if (Int32.TryParse(Console.ReadLine(), out height))
            Convert.ToDecimal(height);
        if (height > 120)                                                                                        // Print an error message stating that if user's input is over 120 then an error pops
            Console.Write("\n height should be less than 120 inches, please press any key to exit:");
        if (height < 5)                                                                                          // Print an error message stating that if user's input is less than 5 then an error pops
            Console.Write("\n height should be greater than 5 inches and if you entered a letter, \n please enter a number next time:\n");
        Console.WriteLine("\n Please enter the person's weight pounds:");                                       // Ask user to enter their weight in pounds
        if (Int32.TryParse(Console.ReadLine(), out weight))
            Convert.ToDecimal(weight);
        if (weight >= 999)                                                                                     // Print an error message if the user's input is more than 999 lbs
            Console.Write("\n weight should be less than 999 pounds, please exit the application");
        if (weight <= 0.5)                                                                                    // Print an error message if the user's input is less than 0.5 lbs
            Console.Write("\n weight should be greater than 0.5, \n if you did not enter a number, please enter a number next time:\n");

        /* Processing */

        bmi = Math.Round(weight / Math.Pow(height, 2) * factorBMI, 1);                                         // Calculates the user's BMI based on their height and weight input

        /* Output */
        Console.WriteLine("************************************************************************************************************************");
        Console.WriteLine("\n the BMI for a " + height + "\" tall person who weighs: " + weight + "lb. is " + bmi); // Print user's height, weight and calculated BMI
        if (bmi < 16)                                                                                              // If BMI is less than 16 then print the message
            Console.WriteLine("\n base on your BMI you are severly underweight");
        else if (bmi <= 18)                                                                                        // If BMI is less than 18 then print the message
            Console.WriteLine("\n base on your BMI you are underweight");
        else if (bmi <= 25)                                                                                       // If BMI is less than 25 then print the message
            Console.WriteLine("\n base on your BMI you are healthy");
        else if (bmi <= 30)                                                                                      // If BMI is less than 30 then print the message
            Console.WriteLine("\n base on your BMI you are overweight");
        else if (bmi > 30)                                                                                      // If BMI is more than 30 then print the message
            Console.WriteLine("\n base on your BMI you are obese");

        /* Exit  */

        Console.WriteLine("\n\n Thanks for using our program, you may now exit this program by presing any key:");// Print message telling the user to press any keys to exit application
        Console.ReadKey();

Add an else branch to your if (Int32.TryParse ...) statements like here: 将else分支添加到您的if(Int32.TryParse ...)语句中,如下所示:

if (!decimal.TryParse(Console.ReadLine(), out height))
{
    Console.WriteLine("Not a valid number"); //etc.
    Console.ReadKey();
    Environment.Exit(0);
}

From your other error-outputs make blocks like here 从您的其他错误输出中,使块如下所示

if (height > 120)
{
    Console.WriteLine("Your text");
    Console.ReadKey();
    Environment.Exit(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM