bool correctInput이 false로 변경되면 do while 루프가 반복되지 않습니다. 입력을 올바르게 입력 할 때까지 반복됩니다. 허용되는 입력은 0보다 큰 정수입니다. 정수이면 try catch를 던지고 correctInput 부울을 false로 변경하여 루프를 발생시킵니다. 정수가 0보다 큰 경우는 correctInput가 false가되어 루프가 발생합니다. 사용자가 올바른 입력을 입력 한 경우에만 루프가 종료됩니다. 입력이 올바르지 않으면 현재 루핑되지 않습니다.Do while while boolean test with C#
int value;
if (!int.TryParse(rawInput, out value)) {
correctInput = false;
}
그리고와 같은 : 당신이 try catch
을 필요가 없습니다,
if (Length <= 0) {
correctInput = false; //Changes correctInput to false if input is less than zero
}
는 C#, 당신은 또한 TryParse
을 사용할 수
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