當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