Java猜谜游戏无限循环

Java猜谜游戏无限循环,java,Java,我的代码有问题,程序将不再播放,但是 也不会结束 在获胜或猜测过多后,代码继续在内部循环 这是我的密码: /** * 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; pub

我的代码有问题,程序将不再播放,但是 也不会结束

在获胜或猜测过多后,代码继续在内部循环

这是我的密码:

/**
 * 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;
                }
            }
        }
    }
}
/**
*此程序将提示用户猜测密码
*此数字将介于1和N之间,其中N是正数
*
*@作者:佩里·查普曼
*/
导入java.util.Scanner;
公共类机密编号2
{
公共静态void main(字符串[]args)
{
int N;
整数随机数;
智力猜测;
整数尝试,最大尝试;
布尔赢=假;
布尔播放=真;
int=1;
尝试=0;
while(播放==true)
{ 
扫描仪输入=新扫描仪(System.in);
System.out.println(“这是一个猜谜游戏”);
System.out.println(“最大尝试次数:”);
maxTries=input.nextInt();
System.out.println(“输入一个介于1和1000之间的值:”);
N=input.nextInt();
随机数=(int)(N*Math.random())+1;
while(win==false)
{
System.out.println(“输入您的猜测:”);
guess=input.nextInt();
尝试++;
如果(猜测==随机数)
{  
System.out.println(“祝贺你!你猜的次数是”+次尝试);
赢=真;
System.out.println(“您想再次播放吗?\n(1)是或(2)否:”;
再次播放=input.nextInt();
如果(再次播放==1){
玩=真;
}
否则如果(再次播放==2){
玩=假;
}
尝试=0;
}
else if(猜测<随机数)
System.out.println(“太低,再次猜测”);
else if(猜测>随机数)
System.out.println(“太高了,再猜一次。”);
else if(trys==maxTries)
{
System.out.println(“您已超过最大尝试次数。\n失败”);
System.out.println(“\n您想再次播放吗?\n(1)是或(2)否:”);
再次播放=input.nextInt();
如果(再次播放==1){
玩=真;
}
否则如果(再次播放==2){
玩=假;
}
尝试=0;
}
}
}
}
}

我想你有两个问题

首先,在成功猜出数字后,您从未将win设置回false。因此,您将永远不会再次进入内部while循环


第二个是if/else-if语句的顺序,在任何情况下,猜测都不能到达最终的else-if。在前三个ifs中,每种情况都将验证为true。

Andrew是正确的,为了循环,条件必须为true。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.");
            }
        }
    }
}
publicstaticvoidmain(字符串[]args){
int N;
整数随机数;
智力猜测;
整数尝试,最大尝试;
布尔丢失;
布尔播放=真;
再次播放int;
尝试=0;
玩的时候{
扫描仪输入=新扫描仪(System.in);
System.out.println(“这是一个猜谜游戏”);
System.out.println(“最大尝试次数:”);
maxTries=input.nextInt();
System.out.println(“输入一个介于1和1000之间的值:”);
N=input.nextInt();
随机数=(int)(N*Math.random())+1;
失去=真实;
同时(失去){
System.out.println(“输入您的猜测:”);
guess=input.nextInt();
尝试++;
如果(猜测==随机数){
System.out.println(“祝贺你!你猜的次数是”+次尝试);
丢失=错误;
System.out.println(“您想再次播放吗?\n(1)是或(2)否:”;
再次播放=input.nextInt();
如果(再次播放==1){
玩=真;
}否则如果(再次播放==2){
玩=假;
}
尝试=0;
}else if(trys==maxTries){
System.out.println(“您已超过最大尝试次数。\n失败”);
System.out.println(“\n您想再次播放吗?\n(1)是或(2)否:”);
再次播放=input.nextInt();
如果(再次播放==1){
玩=真;
}否则如果(再次播放==2){
玩=假;
}
丢失=错误;
尝试=0;
}else if(猜测<随机数){
System.out.println(“太低,再次猜测”);
}else if(猜测>随机数){
System.out.println(“太高了,再猜一次。”);
}
}
}
}
/**
*此程序将提示用户猜测密码
*此数字将介于1和N之间,其中N是正数
*
*@作者:佩里C
*/
导入java.util.Scanner;
公共类密文编号
{
公共静态void main(字符串[]args)
{
int N;
智力猜测;
再次播放int;
智力测验;
整数最大值;
再次播放=1;
尝试=0;
扫描仪输入=新扫描仪(System.in);
while(再次播放==1)
{ 
布尔赢=假;
System.out.println(“这是一个猜谜游戏”);
System.out.println(“您想猜多少次?”);
 /**
   * 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;
        }
      }
    }
}
}