2017-02-15 6 views
0

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); 

    } 

답변

1

, 당신은 길이 보다 제로이지만 0보다 큰 경우 코드는 false로 설정하면 correctInput false로 설정해야합니다.

  if (Length > 0) 
      { 
       correctInput = false; //Changes correctInput to false if input is less than zero 
      } 

이 있어야한다 :이 블록의 의견을 바탕으로

  if (Length <= 0) 
      { 
       correctInput = false; //Changes correctInput to false if input is less than zero 
      } 
+0

그냥 보았다 : ... / – DigitalDulphin

2

나는이 if 바꿀 것 귀하의 코드 :

당신의 설명에서
0

, 당신이 경우 블록에 잘못된 연산자를 가지고 : 당신이 correctInput을 변경하려는 경우

if (Length > 0) 
    { 
      correctInput = false; //Changes correctInput to false if input is less than zero 
    } 

입력이 0보다 작 으면

if (Length < 0) 
0

루프의이 부분에서 문제가되지 않습니까?

if (Length > 0) 
{ 
    correctInput = false; //Changes correctInput to false if input is less than zero 
} 

이렇게 읽으면 안 되니?

if (Length < 0) 
{ 
    correctInput = false; 
}