2014-12-07 2 views
0

게임을 다시 시작하는 방법을 알 수 없습니다. System.out.println ("Ok, 다시 게임 해 봅시다.")이라고 적혀있는 부분을 넣어야한다고 생각합니다. 다시 시작하려면 무엇이 필요합니까? 내가 뭘 놓치고 있니?번호를 추측 한 후에이 숫자 추측 게임을 다시 시작하려면 어떻게합니까?

import java.util.Random; 
import java.util.Scanner; 

public class GuessMyNumber { 

    public static void main(String[] args) { 

     Random Bob = new Random(); 
     int NumToGuess = Bob.nextInt(20); 
     int NumOfTries = 0; 
     Scanner Bill = new Scanner(System.in); 
     int guess; 
     boolean win = false; 

     while (win == false) { 

      System.out.println("I'm thinking of a number between 1 and 20, can you guess it?"); 
      guess = Bill.nextInt(); 
      NumOfTries++; 

      if (guess == NumToGuess) { 
       win = true; 
      } 
      else if (guess < NumToGuess) { 
       System.out.println("Your guess is too low"); 
      } 
      else if (guess > NumToGuess) { 
       System.out.println("Your guess is too high"); 
      } 
     } 

     System.out.println("Congratulations!"); 
     System.out.println("The number was " + NumToGuess); 
     System.out.println("It took you " + NumOfTries + " tries"); 

     while (win == true) { 
      String YesOrNo; 
      YesOrNo = Bill.nextLine(); 
      System.out.println("Would you like to play again? " + YesOrNo); 
      YesOrNo = Bill.nextLine(); 

      if (YesOrNo.equalsIgnoreCase("yes")) 
      { 
       System.out.println("Ok, let's play again."); 
      } 
      else 
      { 
       System.out.println("Ok, maybe next time."); 
      } 
     } 
    } 
} 
+0

당신은 당신이 영원히 루프에 머물 것이 아니라 프로그램 흐름의 시작 부분으로 돌아갈 것을 놓치고있어. – EpicPandaForce

+0

@ crpittman18 그런데'''Bob.nextInt (20)''는 0 (포함)과 20 (제외) 사이의 숫자를 생성합니다. – mezzodrinker

답변

0

두 번째 루프가 문제입니다. 첫 번째 루프를 복사했을 가능성이 있습니다. 두 번째 루프에서는 YesOrNo이 아닌 win을 설정합니다. 그래서 그것은 영원히 반복 할 것입니다.

while (win == true) {   //You test win 
     String YesOrNo; 
     YesOrNo = Bill.nextLine() //You set YesOrNo 
     ... 
    }        //You never set win to anything new so it loops forever 

재시작 문제를 해결하는 일반적인 방법은 두 개가 아닌 하나의 루프를 사용하고 사용자가 종료하고자 할 때 -1을 입력하도록하는 것입니다. 그렇게하면 게임이 끝날 때까지 계속 승리하거나 잃게됩니다.

덧붙여서 YesOrNoyesOrNo이어야합니다. 다만 스타일 문제 일뿐입니다.

0

전체 메서드 본문을 while 루프로 랩핑하여 플래그 (예 : boolean continueGame)가 true인지 확인합니다. 그것에 관하여

import java.util.Random; 
import java.util.Scanner; 

public class GuessMyNumber { 
    public static void main(String[] args) { 
     Random Bob = new Random(); 
     Scanner Bill = new Scanner(System.in); 
     int guess; 
     boolean continueGame = true; // the flag (boolean) that allows us to check if the user wants to continue the game or not 

     while(continueGame) { 
      boolean win = false; 
      int NumToGuess = Bob.nextInt(20); // moved into the while loop because they need to be initialized every time you start the game 
      int NumOfTries = 0; 

      while (win == false) { 

       System.out.println("I'm thinking of a number between 1 and 20, can you guess it?"); 
       guess = Bill.nextInt(); 
       NumOfTries++; 

       if (guess == NumToGuess) { 
        win = true; 
       } 
       else if (guess < NumToGuess) { 
        System.out.println("Your guess is too low"); 
       } 
       else if (guess > NumToGuess) { 
        System.out.println("Your guess is too high"); 
       } 
      } 

      System.out.println("Congratulations!"); 
      System.out.println("The number was " + NumToGuess); 
      System.out.println("It took you " + NumOfTries + " tries"); 

      // As CandiedOrange already noted, you need to remove the while loop which was here. 
      String YesOrNo; 
      YesOrNo = Bill.nextLine(); 
      System.out.println("Would you like to play again? " + YesOrNo); 
      YesOrNo = Bill.nextLine(); 

      if (YesOrNo.equalsIgnoreCase("yes")) 
      { 
       System.out.println("Ok, let's play again."); 
       continueGame = true; 
      } 
      else 
      { 
       System.out.println("Ok, maybe next time."); 
       continueGame = false; 
      } 
     } 
    } 
} 

:이 같은 것을 의미한다. 이 답변에 대한 질문이 있으시면 언제든지 물어보십시오.

+0

나는 당신이 말한 모든 것을했는데 지금은 반복되지만, 승리 한 후에도 게임을 계속할 때 동일한 번호를 유지하고 새로운 번호로 바꾸지 않습니다. – crpittman18

+0

@ crpittman18 그래, 미안해. 내가 만든 루프에''NumToGuess''와''NumOfTries''를 넣는 것을 잊어 버렸습니다. 게시물의 코드가 제대로 작동해야합니다. – mezzodrinker

+0

정말 고마워요! 정말 감사! – crpittman18

0

레이블이있는 BREAK 문을 사용할 수 있습니다. 또는 while 루프가 종료되고 프로그램이 종료 될 때 프로그램을 다시 실행해야합니다.

2

당신은 너무 이런 식으로 뭔가를 시도 할 수 :

public class MainClass { 

public static void main(String[] args) { 
    Random bob = new Random(); 
    Scanner bill = new Scanner(System.in); 

    String yesOrNo = ""; 
    do { 

     int numToGuess = bob.nextInt(20); 
     int numOfTries = 0; 
     int guess = 0; 
     boolean win = false; 

     while (win == false) { 

      System.out 
        .println("I'm thinking of a number between 1 and 20, can you guess it?"); 
      guess = Bill.nextInt(); 
      numOfTries++; 

      if (guess == numToGuess) { 
       win = true; 
      } else if (guess < numToGuess) { 
       System.out.println("Your guess is too low"); 
      } else if (guess > numToGuess) { 
       System.out.println("Your guess is too high"); 
      } 
     } 

     System.out.println("Congratulations!"); 
     System.out.println("The number was " + NumToGuess); 
     System.out.println("It took you " + NumOfTries + " tries"); 

     yesOrNo = bill.nextLine(); 
     System.out.println("Would you like to play again? " + yesOrNo); 
     yesOrNo = bill.nextLine(); 

    } while (yesOrNo.equalsIgnoreCase("yes")); 

    System.out.println("Ok, maybe next time."); 
    System.exit(0); 

} 

}

관련 문제