2017-12-05 1 views
-1

숫자 (1-10)을 선택하고 사용자가 추측하는 프로그램을 만드는 프로젝트가 있습니다. 너무 높다면 너무 낮게 추측하면 너무 똑같습니다. 이제 실수를 방지하기 위해 try 블록을 추가해야합니다. 코드를 보면 내가 가지고있는 코드를 볼 수 있지만 코드가 실행되면 catch가 끝납니다. 어떻게 루프백 할 수 있습니까?try-catch를 사용하여 while 루프로 돌아 가기 Java

import java.util.*; 
    import java.lang.*; 

    class GuessDR { 

     public static void main(String[] args){ 



      System.out.println("Welcome to my number guessing game!"); 

      int maxnum = 10; 
      Scanner input = new Scanner(System.in); 
      Random rand = new Random(); 
      int number = rand.nextInt(maxnum); 
      int tries = 0;   
      int guess; 
      boolean win = false; 

      try{ 

       while (win == false){ 



        System.out.println("Guess a number between 1 and "+      
        maxnum +": "); 
        guess = input.nextInt(); 
        tries++;  
        if (guess == number){ 
         win = true; 
        } 

        else if(guess < number){ 
         System.out.println("Number is to low, tray again"); 

        } 

        else if(guess > number){ 
         System.out.println("Number is to high, try again"); 

        } 

       } 
      } 

      catch (InputMismatchException e) { 

       System.out.println("Enter numerical guess");    

      } 
    System.out.println("You win!"); 
    System.out.println("It took you "+ tries + " tries."); 

    } 
} 
+0

while 루프 내에서 try-catch 블록을 움직여보십시오. – AJNeufeld

+0

쉬운 방법? 'try'와'catch' 비트를 루프 안으로 이동하십시오. : P – cHao

+0

다음 질문을 기대하면서 (새로운 추측은 계속 'InputMismatchException'을 생성합니다.) [이 질문과 답변] (https://stackoverflow.com/questions/29811339/cannot-figure-out-how-to- catch-inputmismatchexception) – AJNeufeld

답변

1

버퍼에서 잘못된 입력을 취소 그냥 while 루프로 시도 - 캐치를 이동하고 또한 당신의 catch 블록 내부의 적절한 input.nextLine() 방법을 제공한다 : while 루프로

import java.util.*; 
    import java.lang.*; 

    class GuessDR { 

     public static void main(String[] args) { 

      System.out.println("Welcome to my number guessing game!"); 

      int maxnum = 10; 

      Random rand = new Random(); 
      int number = rand.nextInt(maxnum); 
      int tries = 0; 
      int guess; 
      boolean win = false; 
      Scanner input = new Scanner(System.in); 

      while (win == false) { 
       try { 

        System.out.println("Guess a number between 1 and " + maxnum + ": "); 
        guess = input.nextInt(); 
        tries++; 
        if (guess == number) { 
         win = true; 
        } 

        else if (guess < number) { 
         System.out.println("Number is to low, tray again"); 

        } 

        else if (guess > number) { 
         System.out.println("Number is to high, try again"); 

        } 

       } catch (InputMismatchException e) { 
        input.nextLine(); 
        System.out.println("Enter numerical guess"); 

       } 
      } 

      System.out.println("You win!"); 
      System.out.println("It took you " + tries + " tries."); 

     } 
    } 
+0

아, 내 코드는 괜찮은 것 같습니다. 스캐너를 while 루프로 이동하여 무한 루프를 막는 것에 대해 언급하는 것을 잊어 버렸습니다. 올바른 편집을 위해가는 길에. –

+0

나는 너와 다른 뭔가를 했어야 만했다. 나는 원래 그 일을 시도했다. 그러나 무한 루프가 발생했습니다. 코드를 복사하면 정상적으로 작동합니다. –

+0

내 대답이 옳다고 생각하면 그것을 상향 투표를 고려하고 그것을 받아들이려면 왼쪽에 똑딱. –

0

이동 시도 - 캐치 무한 루프를 예방할 스캐너를 다루십시오. 사용해보기 : https://stackoverflow.com/a/3572233/5964489

while (win == false) 
     { 

      System.out.println("Guess a number between 1 and " 
        + maxnum + ": "); 
      try 
      { 
       guess = input.nextInt(); 
       tries++; 
       if (guess == number) 
       { 
        win = true; 
       } else if (guess < number) 
       { 
        System.out.println("Number is to low, tray again"); 

       } else if (guess > number) 
       { 
        System.out.println("Number is to high, try again"); 

       } 

      } catch (InputMismatchException e) 
      { 
       System.out.println("Enter numerical guess"); 
       input.next(); 
      } 
     } 
관련 문제