2017-04-12 1 views
0

코드는 입력이 숫자가 아닐 때 알려주지 만 동시에 코드가있는 경우 else를 씁니다.
나는 그것이 최고의 double 변수에서 doubleNr에 0을 할당하는 것과 관련이 있다고 믿지만, 그렇지 않다면 while 조건에 할당되지 않은 로컬 변수 'doubleNr'을 사용합니다.
또한 어디서 쓰겠습니까? doubleNr = myMethod (intNr); 라인?
try 블록이나 catch 블록과 if 블록 사이에 있습니까?While 루프 및 try and catch

int intNr; 
double doubleNr = 0; 

while (doubleNr < 20 || doubleNr > 30) 
{ 
    Console.WriteLine("Enter your number: "); 
    string strNr = Console.ReadLine(); 
    try 
    { 
     intNr = Convert.ToInt32(strNr); 
     doubleNr = myMethod(intNr); // Should this line go here? 
    } 
    catch 
    { 
     Console.WriteLine("Number must be INT"); 
    } 
    // or should it go here? 

    if (doubleNr < 20) 
    { 
     Console.WriteLine("Try a higher number."); 
    } 
    else if (doubleNr > 30) 
    { 
     Console.WriteLine("Try a lower number."); 
    } 
} 
Console.WriteLine("That is a good number."); 
+4

당신은 이동 – Pikoh

+0

프로그램의 흐름을 제어하기 위해 try/catch를 사용하지 말아야합니다 귀하의 경우/다른 사용자의 try 블록 내부의 문을 당신이 <20 – FCin

+2

'doubleNr 잘하는 경우는 || doubleNr <30' 이것은 의미가 없습니다. doubleNr <30'을 말하는 것과 같습니다. 왜 두 가지 조건을 사용합니까? – Toby

답변

0

이것은 아마도 해답이 될만한 것은 아니지만 코드에 몇 가지 사항을 지적하고자합니다. try-catch을 사용할 것으로 예상되므로 간단하게 retry-catch을 구현할 수 있습니다. 이렇게하려면 @Pikoh가 말한대로 프로그램의 흐름을 제어하는 ​​try-catch 논리에 while(true)을 사용해야합니다.

public static void Main(string[] args) 
{ 
    int intNr; 
    double doubleNr = 0; 
    while(true) 
    { 
     try 
     { 
      Console.WriteLine("Enter your number: "); 
      string strNr = Console.ReadLine(); 
      doubleNr = Int32.Parse(strNr); 
      if(doubleNr < 20) 
      { 
       throw new Exception("Try a higher number."); 
      } 
      else if(doubleNr > 30) 
      { 
       throw new Exception("Try a lower number."); 
      } 
      else 
      { 
       Console.WriteLine("That is a good number."); 
       break; 
      } 
     } 
     catch(FormatException e) 
     { 
      Console.WriteLine("Enter a valid number."); 
      continue; 
     } 
     catch(Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
      continue; 
     } 
    } 
} 

여기에서 당신은 당신의 프로그램의 try-catch 논리를 제어 할 while 루프 조절 아니라고 볼 수 있습니다. 물론이 작업을 수행하는 다른 방법이 있지만 이는 코드를 재구성하는 것입니다.

잘 알고 계시 겠지만이 또한 더 나은 방법이 있습니다. 이 경우 가장 좋은 Int32.TryParse()을 사용할 수 있습니다. try-catch 블록을 사용할 필요가 없습니다.

public static void Main(string[] args) 
{ 
    int intNr; 
    double doubleNr = 0; 
    while(true) 
    { 
     Console.WriteLine("Enter your number: "); 
     string strNr = Console.ReadLine(); 
     int value; 
     if(Int32.TryParse(strNr, out value)) 
     { 
      doubleNr = value; 
     } 
     else 
     { 
      Console.WriteLine("Enter a valid number: "); 
      continue; 
     } 
     if(doubleNr < 20) 
     { 
      Console.WriteLine("Try a higher number."); 
      continue; 
     } 
     else if(doubleNr > 30) 
     { 
      Console.WriteLine("Try a lower number."); 
      continue; 
     } 
     else 
     { 
      Console.WriteLine("That is a good number."); 
      break; 
     } 
    } 
} 
-1

당신이 예외 또는 기타 조건의 경우 전류 루프 단계를 건너 continue 문을 사용합니다.

continue 문은 나타나는 while, do, for 또는 foreach 문을 포함하는 의 다음 반복으로 제어를 전달합니다.

이 코드 예제는 continue 문입니다.

int intNr; 
double doubleNr = 0; 

while (doubleNr < 20 || doubleNr > 30) 
{ 
    Console.WriteLine("Enter your number: "); 
    string strNr = Console.ReadLine(); 
    try 
    { 
     intNr = Convert.ToInt32(strNr); 
     // if you want to catch any exceptions in this method then leave it there 
     // otherwise you can move it outside try/catch block    
     doubleNr = myMethod(intNr); 

    } 
    catch 
    { 
     Console.WriteLine("Number must be INT"); 
     continue; // return to the beginning of your loop 
    } 

    if (doubleNr < 20) 
    { 
     Console.WriteLine("Try a higher number."); 
     continue; // return to the beginning of your loop 
    } 
    // do not need else since code wont reach this line if doubleNr is < 20 
    if (doubleNr > 30) 
    { 
     Console.WriteLine("Try a lower number.");       
     continue; // return to the beginning of your loop 
    } 
} 
Console.WriteLine("That is a good number.");