如何使用户输入的程序状态无效,然后在按下Enter后关闭?我有第一个错误,指出用户输入的值是错误的,给他们另一个尝试,但如果用户键入相同/无效的数字,它将重复最后一件事。我怎样才能限制再试一次然后如果用户没有给出有效的输入,那就错了?代码如下:
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 :(得分:3)
鉴于这是一项任务,我不确定你的教授是否与我的一样,我们只能使用我们在课堂上学到的东西。也就是说,你可能想看看double.tryparse https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx
如果输入可解析为double,则函数将返回bool,如果输入无效,则返回false。上面的MSDN linke上的代码示例应该给你一个很好的开始。
答案 1 :(得分: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 :(得分:2)
查看代码的逻辑。 while循环将不断循环,直到表达式求值为false。如果您希望尽早终止循环,则需要通过更改条件表达式,使用不同类型的循环或使用break语句来修改循环。
Here is some reference material from Microsoft's website.
仔细思考你的问题然后问问自己:如果你手动这样做,你会怎么做?如果你在电脑前,你会采取什么合乎逻辑的步骤?您将做出哪些决定以及做出这些决定需要哪些信息?
答案 3 :(得分: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);
}
}
请记住,它不会检测用户是否输入字母数字或特殊字符,但您明白了。