我在我的上一个while循环中获得了无限循环,状态如下:while(Valid3 == false)... 我希望收到console.writeline(“无效输入,请重试”),直到用户满足参数为止,但是当我输入无效输入时,我会收到无限循环。虽然我觉得这里使用的逻辑与先前使用的循环中的逻辑相同。下面是我的所有代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalculatorApp
{
class Program
{
static void Main(string[] args)
{
// Declare my and then initialize to zero.
float num1 = 0; float num2 = 0;
// Display title as the C# console calculator app.
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
// Ask the user to type the first number.
//Console.WriteLine("Type a number, and then press Enter");
Console.WriteLine("Type a number, and then press Enter");
List<string> validChars = new List<string>() { "a", "s", "d", "m"};
double negSqrt = Math.Sqrt(-1);
bool Valid = false;
bool Valid2 = false;
bool Valid3 = false;
float Number;
while (Valid == false)
{
string Input = Console.ReadLine();
if (!float.TryParse(Input, out Number))
{
Console.WriteLine("Not an integer, please try again.");
}
else
{
Valid = true;
num1 = (float)Convert.ToDecimal(Input);
}
}
// Ask the user to type the second number.
Console.WriteLine("Type another number, and then press Enter");
while (Valid2 == false)
{
string Input2 = Console.ReadLine();
if (!float.TryParse(Input2, out Number))
{
Console.WriteLine("Not an integer, please try again.");
}
else
{
Valid2 = true;
num2 = (float)Convert.ToDecimal(Input2);
}
}
// Ask the user to choose an option.
Console.WriteLine("Choose an option from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");
string o = Console.ReadLine();
while (Valid3 == false)
{
foreach (string s in validChars)
{
if (!o.Contains(s))
{
Console.WriteLine("Invalid input please try again");
}
else
{
Valid3 = true;
switch (o)
{
case "a":
Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2));
break;
case "s":
Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2));
break;
case "m":
Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2));
break;
case "d":
Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
break;
}
}
}
}
// Use a switch statement to do the math.
// Wait for the user to respond before closing.
Console.Write("Press any key to close the Calculator console app...");
Console.ReadKey();
}
}
}
答案 0 :(得分:1)
在您的while循环内移动Console.ReadLine()
while (Valid3) {
string o = Console.ReadLine();
...
为什么会进行无限循环
您要输入,输入后,进入while循环。如果输入无效,则不再需要其他输入,而是一次又一次地遍历同一输入。
答案 1 :(得分:1)
您必须在循环内读取运算符,否则,您将始终反复比较相同的无效输入。另外,您可以简化测试。无需对有效输入列表进行测试,只需在switch语句中添加默认大小写即可。如果没有其他情况匹配,它将被执行。
bool isOperatorValid;
do {
isOperatorValid = true;
switch (Console.ReadLine()) {
case "a":
Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2));
break;
case "s":
Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2));
break;
case "m":
Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2));
break;
case "d":
Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
break;
default:
Console.WriteLine("Invalid input please try again");
isOperatorValid = false;
break;
}
} while (!isOperatorValid);
isOperatorValid
比Valid3
更清晰。我还使用了一条do-while语句,能够在循环结束时测试条件,因为开始循环时我们不知道其结果。