繁体   English   中英

Java Guessing游戏无限循环

[英]Java Guessing Game infinite loop

我的代码存在问题,程序将无法再次播放,但也不会结束。

在获胜或过多猜测之后,代码将继续在内部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;
                }
            }
        }
    }
}

安德鲁是对的,为了循环,条件必须为真。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.");
            }
        }
    }
}

我认为您有两个问题。

首先是在成功猜出数字之后,您再也不会将赢取为假。 因此,您永远不会再进入内部while循环。

第二个是if / else if语句的顺序,永远不会出现猜测可以到达最终else if的情况。 在前面三个if之一中,每种情况都将被验证为真实。

 /**
   * 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;
        }
      }
    }
}
}

找到了问题,以为我可以发布答案,以防其他人偶然发现该线程! 谢谢您的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM