2017-02-12 98 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; 
     } 
     } 
    } 
} 
} 

发现问题,以为我会发布答案的情况下,任何人都可能无意中发现这个线程!谢谢您的帮助!