用户输入无效后程序结束
Program ends after user input is invalid
如何使程序状态为用户输入无效,然后在按下回车后关闭?我有第一个错误,指出用户输入的值是错误的,再试一次,但如果用户输入 same/invalid 数字,它将重复最后一件事。我怎样才能让它限制再尝试一次,然后如果用户没有提供有效的输入,它就会正确和错误?代码如下:
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();
}
}
}
鉴于这是一项作业,我不确定您的教授是否像我的教授一样,我们只能使用我们在 class 中学到的知识。也就是说,您可能想查看 double.tryparse https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx
如果输入可解析为双精度值,则函数将 return 返回布尔值;如果输入无效,则返回 false。上面 MSDN 链接上的代码示例应该会给您一个良好的开端。
试试这个代码
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);
}
}
请记住,它不会检测用户输入的是字母数字还是特殊字符,但您明白了。
您显然是编码世界的新手,这看起来像是一道家庭作业题。但是,我怀疑您突出显示的问题的根源在于,当您调用无法转换输入的 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...");
}
看看你的代码逻辑。 while 循环将不断循环,直到表达式的计算结果为 false。如果您希望提前终止循环,则需要通过更改条件表达式、使用不同类型的循环或使用 break 语句来修改循环。
Here is some reference material from Microsoft's website.
想一想您的问题并问问自己:如果您手动执行此操作,您将如何执行此操作?如果你处在计算机的位置,你会采取什么逻辑步骤?您会做出什么决定以及做出这些决定需要哪些信息?
如何使程序状态为用户输入无效,然后在按下回车后关闭?我有第一个错误,指出用户输入的值是错误的,再试一次,但如果用户输入 same/invalid 数字,它将重复最后一件事。我怎样才能让它限制再尝试一次,然后如果用户没有提供有效的输入,它就会正确和错误?代码如下:
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();
}
}
}
鉴于这是一项作业,我不确定您的教授是否像我的教授一样,我们只能使用我们在 class 中学到的知识。也就是说,您可能想查看 double.tryparse https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx
如果输入可解析为双精度值,则函数将 return 返回布尔值;如果输入无效,则返回 false。上面 MSDN 链接上的代码示例应该会给您一个良好的开端。
试试这个代码
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);
}
}
请记住,它不会检测用户输入的是字母数字还是特殊字符,但您明白了。
您显然是编码世界的新手,这看起来像是一道家庭作业题。但是,我怀疑您突出显示的问题的根源在于,当您调用无法转换输入的 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...");
}
看看你的代码逻辑。 while 循环将不断循环,直到表达式的计算结果为 false。如果您希望提前终止循环,则需要通过更改条件表达式、使用不同类型的循环或使用 break 语句来修改循环。
Here is some reference material from Microsoft's website.
想一想您的问题并问问自己:如果您手动执行此操作,您将如何执行此操作?如果你处在计算机的位置,你会采取什么逻辑步骤?您会做出什么决定以及做出这些决定需要哪些信息?