gpt4 book ai didi

Java猜谜游戏无限循环

转载 作者:行者123 更新时间:2023-12-01 08:53:44 25 4
gpt4 key购买 nike

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

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

最佳答案

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

关于Java猜谜游戏无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42193992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com