2016-03-08 52 views
1

如何讓程序狀態爲用戶輸入無效,然後在輸入完成後關閉?我有第一個錯誤,指出用戶輸入的值是錯誤的,給他們另一次嘗試,但如果用戶鍵入相同/無效的數字,它會重複最後一件事。我怎麼能限制到一個更多的嘗試,然後如果用戶沒有提供有效的條目,它會正確和錯誤?代碼如下:用戶輸入無效後程序結束

 string First; 
     string Last; 
     First = "Cristiano"; 
     Last = " Ronaldo"; 
     Console.Write("Please enter student name <First Last>: "); 
     Console.WriteLine(First + Last); 

     Console.WriteLine(" "); 

     Console.WriteLine("*************NOTE**********************************************"); 
     Console.WriteLine("*** Be sure to include decimal point for scores.   ***"); 
     Console.WriteLine("***  !!!All score should range from 0.00 to 100.00 !! ***"); 
     Console.WriteLine("***               ***"); 
     Console.WriteLine("*** For example : 80.50          ***"); 
     Console.WriteLine("***************************************************************"); 

     Console.WriteLine(" "); 

     double Exam_1 = -1; 
     double Exam_2; 
     double Exam_3; 
     double Assignment_1; 
     double Assignment_2; 

     Console.Write("Please enter score for Exam 1 <Example: 100.0>: "); 
     Exam_1 = Convert.ToDouble(Console.ReadLine()); 

     while (Exam_1 < 0.0 || Exam_1 > 100.0) 
     { 
      Console.Write("Exam score cannot be less than 0. or greater than 100.0. Please re-enter the score for Exam 1 <Example: 95.0>:"); 
      Exam_1 = Convert.ToDouble(Console.ReadLine()); 
     } 

     Console.Write("Please enter score for Exam 2 <Example: 0.0>: "); 
     Exam_2 = Convert.ToDouble(Console.ReadLine()); 

     while (Exam_2 < 0.0 || Exam_2 > 100.0) 
     { 
      Console.Write("Exam score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Exam 2 <Example: 95.0>:"); 
      Exam_2 = Convert.ToDouble(Console.ReadLine()); 
     } 

     Console.Write("Please enter score for Exam 3 <Example: 60.8>: "); 
     Exam_3 = Convert.ToDouble(Console.ReadLine()); 

     while (Exam_3 < 0.0 || Exam_3 > 100.0) 
     { 
      Console.Write("Exam score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Exam 3 <Example: 95.0>:"); 
      Exam_3 = Convert.ToDouble(Console.ReadLine()); 
     } 

     Console.WriteLine(" "); 

     Console.Write("Please enter score for Assignment 1 <Example: 100.0>: "); 
     Assignment_1 = Convert.ToDouble(Console.ReadLine()); 

     while (Assignment_1 < 0.0 || Exam_2 > 100.0) 
     { 
      Console.Write("Assignment score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Assignment 1 <Example: 95.0>:"); 
      Assignment_1 = Convert.ToDouble(Console.ReadLine()); 
     } 

     Console.Write("Please enter score for Assignment 2 <Example: 23.46>: "); 
     Assignment_2 = Convert.ToDouble(Console.ReadLine()); 

     while (Assignment_2 < 0.0 || Assignment_2 > 100.0) 
     { 
      Console.Write("Assignment score can not be less than 0.0 or greater than 100.0. Please re-enter the score for Assignment 2 <Example: 56.0>: "); 
      Assignment_2 = Convert.ToDouble(Console.ReadLine()); 
     } 

     Console.WriteLine(" "); 

     Console.WriteLine(" -------------- OUTPUT ---------------"); 

     Console.WriteLine(" "); 

     Console.Write("Student: "); 
     Console.WriteLine(First + Last); 

     Console.WriteLine(" "); 



















     Console.Write("Press any key to continue . . . "); 
     Console.ReadLine(); 
    } 
} 

}

回答

0

試試這個代碼

static void Main(string[] args) 
    { 
     string First; 
     string Last; 
     First = "Cristiano"; 
     Last = " Ronaldo"; 
     Console.Write("Please enter student name <First Last>: "); 
     Console.WriteLine(First + Last); 

     Console.WriteLine(" "); 

     Console.WriteLine("*************NOTE**********************************************"); 
     Console.WriteLine("*** Be sure to include decimal point for scores.   ***"); 
     Console.WriteLine("***  !!!All score should range from 0.00 to 100.00 !! ***"); 
     Console.WriteLine("***               ***"); 
     Console.WriteLine("*** For example : 80.50          ***"); 
     Console.WriteLine("***************************************************************"); 

     Console.WriteLine(" "); 

     double Exam_1 = -1; 
     double Exam_2; 
     double Exam_3; 
     double Assignment_1; 
     double Assignment_2; 

     Console.Write("Please enter score for Exam 1 <Example: 100.0>: "); 
     Exam_1 = Convert.ToDouble(Console.ReadLine()); 
     var exitProgram = false; 
     var errorCount = 0; 

     while (Exam_1 < 0.0 || Exam_1 > 100.0) 
     { 
      Console.Write("Exam score cannot be less than 0. or greater than 100.0. Please re-enter the score for Exam 1 <Example: 95.0>:"); 
      Exam_1 = Convert.ToDouble(Console.ReadLine()); 
      ++errorCount; 
      ErrorCount(errorCount); 
     } 

     Console.Write("Please enter score for Exam 2 <Example: 0.0>: "); 
     Exam_2 = Convert.ToDouble(Console.ReadLine()); 
     errorCount = 0; 
     while (Exam_2 < 0.0 || Exam_2 > 100.0) 
     { 
      Console.Write("Exam score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Exam 2 <Example: 95.0>:"); 
      Exam_2 = Convert.ToDouble(Console.ReadLine()); 
      ++errorCount; 
      ErrorCount(errorCount); 
     } 

     Console.Write("Please enter score for Exam 3 <Example: 60.8>: "); 
     Exam_3 = Convert.ToDouble(Console.ReadLine()); 
     errorCount = 0; 
     while (Exam_3 < 0.0 || Exam_3 > 100.0) 
     { 
      Console.Write("Exam score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Exam 3 <Example: 95.0>:"); 
      Exam_3 = Convert.ToDouble(Console.ReadLine()); 
      ++errorCount; 
      ErrorCount(errorCount); 
     } 

     Console.WriteLine(" "); 

     Console.Write("Please enter score for Assignment 1 <Example: 100.0>: "); 
     Assignment_1 = Convert.ToDouble(Console.ReadLine()); 
     errorCount = 0; 
     while (Assignment_1 < 0.0 || Exam_2 > 100.0) 
     { 
      Console.Write("Assignment score cannot be less than 0.0 or greater than 100.0. Please re-enter the score for Assignment 1 <Example: 95.0>:"); 
      Assignment_1 = Convert.ToDouble(Console.ReadLine()); 
      ++errorCount; 
      ErrorCount(errorCount); 
     } 

     Console.Write("Please enter score for Assignment 2 <Example: 23.46>: "); 
     Assignment_2 = Convert.ToDouble(Console.ReadLine()); 
     errorCount = 0; 
     while (Assignment_2 < 0.0 || Assignment_2 > 100.0) 
     { 
      Console.Write("Assignment score can not be less than 0.0 or greater than 100.0. Please re-enter the score for Assignment 2 <Example: 56.0>: "); 
      Assignment_2 = Convert.ToDouble(Console.ReadLine()); 
      ++errorCount; 
      ErrorCount(errorCount); 
     } 

     Console.WriteLine(" "); 

     Console.WriteLine(" -------------- OUTPUT ---------------"); 

     Console.WriteLine(" "); 

     Console.Write("Student: "); 
     Console.WriteLine(First + Last); 

     Console.WriteLine(" "); 
     Console.Write("Press any key to continue . . . "); 
     Console.ReadLine(); 
    } 

    public static void ErrorCount(int errorCount) 
    { 
     if (errorCount > 0) 
     { 
      Console.Write("Error count too much ! . . . "); 
      Console.Write("Press any key to exit . . . "); 
      Console.ReadKey(); 
      Environment.Exit(0); 
     } 

    } 

請記住,如果用戶輸入一個字母數字號碼或特殊字符,但你的想法不檢測。

3

鑑於這是一個任務,我不知道,如果你的教授是像我這樣的事情,我們只能用我們在課堂上所學到。也就是說,您可能想要查看double.tryparse https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx

如果輸入可以解析爲double,則函數將返回bool,如果輸入無效,則返回false。上面的MSDN代碼示例應該給你一個很好的開始。

3

你顯然是編程界的新手,這看起來像一個家庭作業問題。但是,我懷疑你突出顯示的問題根源在於,如果調用Convert.ToDouble(Console.ReadLine())(其中輸入無法轉換),則會拋出異常並且不會捕獲它。

你可以閱讀更多有關異常的位置:https://msdn.microsoft.com/en-us/library/ms173160.aspx

要解決,我會實現這樣的事情:

Console.Write("Please enter score for Exam 1 <Example: 100.0>: "); 
while(!Double.TryParse(Console.ReadLine(), out Exam_1)) 
{ 
     Console.Write("Try again..."); 
} 
2

看看你的代碼的邏輯。 while循環會持續循環,直到表達式計算結果爲false。如果您希望儘早終止循環,則需要通過更改條件表達式,使用不同類型的循環或使用break語句來修改循環。

Here is some reference material from Microsoft's website.

想通過你的問題,並問自己:你會怎樣做,如果你是手動這樣做呢?如果你在電腦的地方,你會採取什麼邏輯步驟?你會做出什麼樣的決定,你需要哪些信息來做出這些決定?

相關問題