2017-02-12 97 views
0

我遇到了我的代碼問題,程序不會再次播放,但 也不會結束。Java猜測遊戲無限循環

代碼在獲勝或猜測後繼續在inner while循環中循環。

這裏是我的代碼:

/** 
* This program will prompt the user to guess a secret number 
* This number will is between 1 and N, where N is a postive number 
* 
* @author: Perry Chapman 
*/ 

import java.util.Scanner; 

public class SecretNumber2 
{ 
    public static void main(String [] args) 
    { 
     int N; 
     int randomNumber;  
     int guess; 
     int tries, maxTries; 

     boolean win = false; 
     boolean playing = true; 
     int playAgain = 1; 
     tries = 0; 

     while(playing == true) 
     { 
      Scanner input = new Scanner(System.in); 

      System.out.println("This is a guessing game."); 
      System.out.println("What is the max number of tries: "); 
      maxTries = input.nextInt(); 
      System.out.println("Enter a value between 1 and 1000: "); 
      N = input.nextInt(); 

      randomNumber = (int)(N * Math.random()) + 1; 

      while (win == false) 
      { 
       System.out.println("Enter your guess: ");      
       guess = input.nextInt(); 
       tries++; 

       if (guess == randomNumber) 
       { 
        System.out.println("Congratulations! The number of guesses it took you was " + tries); 
        win = true; 
        System.out.println("Would you like to play again? \n (1)Yes or (2)No: "); 
        playAgain = input.nextInt(); 

        if(playAgain == 1) { 
         playing = true; 
        } 

        else if (playAgain == 2) { 
         playing = false; 
        } 
        tries = 0; 
       } 

       else if(guess < randomNumber) 
        System.out.println("Too low, guess again."); 

       else if(guess > randomNumber)       
        System.out.println("Too high, guess again."); 

       else if(tries == maxTries) 
       { 
        System.out.println("You have exceeded the max number of tries. \nYou lose."); 
        System.out.println("\nWould you like to play again?\n (1)Yes or (2)No: "); 
        playAgain = input.nextInt(); 

        if(playAgain == 1) { 
         playing = true; 
        } 

        else if(playAgain == 2) { 
         playing = false; 
        } 

        tries = 0; 
       } 
      } 
     } 
    } 
} 
+0

你爲什麼問「最大嘗試次數是多少?」 – Sedrick

+0

澄清問題 –

回答

0

我認爲你有兩個問題。

第一個是,成功猜出你從未設置的數字回覆爲false。所以你不會再次進入inner while循環。

第二個是if/else if語句的排序,從來沒有一個猜想可以達到最終else的情況if。在前三個ifs中的任何一箇中,每個情況都將驗證爲真。

1

安德魯是對的,爲了循環條件需要是真的。if else語句需要按正確的順序。這是一個修復,我希望它會有所幫助。

public static void main(String[] args) { 
    int N; 
    int randomNumber; 
    int guess; 
    int tries, maxTries; 

    boolean lose; 
    boolean playing = true; 
    int playAgain; 
    tries = 0; 

    while (playing) { 
     Scanner input = new Scanner(System.in); 

     System.out.println("This is a guessing game."); 
     System.out.println("What is the max number of tries: "); 
     maxTries = input.nextInt(); 
     System.out.println("Enter a value between 1 and 1000: "); 
     N = input.nextInt(); 

     randomNumber = (int) (N * Math.random()) + 1; 
     lose = true; 

     while (lose) { 
      System.out.println("Enter your guess: "); 
      guess = input.nextInt(); 
      tries++; 

      if (guess == randomNumber) { 
       System.out.println("Congratulations! The number of guesses it took you was " + tries); 
       lose = false; 
       System.out.println("Would you like to play again? \n (1)Yes or (2)No: "); 
       playAgain = input.nextInt(); 

       if (playAgain == 1) { 
        playing = true; 
       } else if (playAgain == 2) { 
        playing = false; 
       } 
       tries = 0; 
      } else if (tries == maxTries) { 
       System.out.println("You have exceeded the max number of tries. \nYou lose."); 
       System.out.println("\nWould you like to play again?\n (1)Yes or (2)No: "); 
       playAgain = input.nextInt(); 

       if (playAgain == 1) { 
        playing = true; 
       } else if (playAgain == 2) { 
        playing = false; 
       } 
       lose = false; 
       tries = 0; 
      } else if (guess < randomNumber) { 
       System.out.println("Too low, guess again."); 
      } else if (guess > randomNumber) { 
       System.out.println("Too high, guess again."); 
      } 
     } 
    } 
} 
0
/** 
    * This program will prompt the user to guess a secret number 
    * This number will is between 1 and N, where N is a postive number 
    * 
* @author: Perry C 
*/ 

import java.util.Scanner; 

public class SecretNumber 
{ 
public static void main(String [] args) 
{ 
    int N;  
    int guess; 
    int playAgain; 
    int tries; 
    int maxTries; 

    playAgain = 1; 
    tries = 0; 

    Scanner input = new Scanner(System.in); 

    while(playAgain == 1) 
     { 
     boolean win = false; 
     System.out.println("This is a guessing game."); 
     System.out.println("How many guesses would you like?"); 
     maxTries = input.nextInt(); 
     System.out.println("Enter a value between 1 and 1000: "); 
     N = input.nextInt(); 

     int randomNumber = (int)(N * Math.random()) + 1; 

    while (win == false) 
     { 
     System.out.println("Enter your guess: ");      
     guess = input.nextInt(); 
     tries++; 

     if(guess == randomNumber) 
     { 
      System.out.println("Congratulations! The number of guesses it took you was \t" + tries); 
      win = true; 
      System.out.println("Would you like to play again(1 or 2):"); 
      playAgain = input.nextInt(); 
      tries = 0; 
     } 

     else if(guess < randomNumber) 
      System.out.println("Too low, guess again."); 

     else if(guess > randomNumber)       
      System.out.println("Too high, guess again."); 

     if(tries == maxTries) 
     { 
      System.out.println("You have exceeded the max number of tries. \n You lose."); 
      System.out.println("Would you like to play again(1 or 2):"); 
      playAgain = input.nextInt(); 
      tries = 0; 
      win = true; 
     } 
     } 
    } 
} 
} 

發現問題,以爲我會發布答案的情況下,任何人都可能無意中發現這個線程!謝謝您的幫助!