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