2016-10-10 52 views
0

我不知道如何要求用戶按任何鍵退出時,他們輸入一個無效的輸入。c#我如何要求用戶按任何鍵退出時,他們輸入一個無效的輸入

每當我嘗試把Console.ReadKey(),該程序只是繼續前進。我不知道如何在用戶沒有輸入有效的輸入時停止。

下面是截圖 enter image description here

下面的代碼

/* 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(); 
+0

請發佈您的代碼。 – A3006

+0

如果用戶只需按下「任意鍵」退出,他們如何向控制檯提供無效輸入? 將您的代碼直接複製並粘貼到問題中。概述你想要做什麼,不想要的行爲是什麼,以及你到目前爲止嘗試過的任何事情。 – Lodestone6

+0

歡迎來到Stack Overflow!請參閱[問]和[mcve]。 – Mat

回答

0

一個else分支添加到您的,如果(Int32.TryParse ......)之類的語句在這裏:

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

從你的其他錯誤輸出使塊像這裏

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

嘿謝謝你,還有一個問題。每當我嘗試輸入一個小數時,它會如何提示writeline並退出應用程序? –

+0

@ChowLife也許這個小數不能轉換成Int32? –

+0

也許,如果你期望有一個十進制輸入,你最好使用decimal.TryParse而不是Int32.TryParse。然後你可以保存轉換。我編輯了我的代碼示例。也可能是雙倍而不是十進制給你足夠的精度來達到你的目的,但那必須是你的決定。 –

相關問題