当bool correctInput变为false时,我的while循环将不会循环。它应该循环直到输入从使用中正确输入。可接受的输入是大于零的整数。任何是整型的都会抛出try catch,并将correctInput boolean更改为false,从而导致循环。如果整数不大于零,则correctInput将变为false,导致循环。只有当用户输入正确的输入时,循环才会退出。当输入不正确时,它当前不会循环。虽然不会循环使用布尔测试C#
private static void InputMangementShapeSquare()
{
bool correctInput = true;
int Length = 0;
string rawInput;
do
{
correctInput = true;
Console.WriteLine("Enter the Length: ");
rawInput = Console.ReadLine();
try
{
Length = Int32.Parse(rawInput);
if (Length > 0)
{
correctInput = false; //Changes correctInput to false if input is less than zero
}
}
catch (Exception exception)
{
correctInput = false; //Changes correctInput flag to false when rawinput failed to be converted to integer.
Console.WriteLine("input must be an interger greater than zero.");
}
} while (correctInput == false);
Square square = new Square(Length);
}
只是看到了...... / – DigitalDulphin