gpt4 book ai didi

java - 我点击 "run"它继续加载,没有运行或停止的迹象

转载 作者:行者123 更新时间:2023-12-01 18:24:29 26 4
gpt4 key购买 nike

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

public static void main (String [] args) {

Random rand = new Random ();
int numberToGuess = rand.nextInt (100);
int numberOfTries = 0;
Scanner input = new Scanner (System.in);
int guess;

boolean win = false;

while (win == false); {

System.out.println("Guess a number between 1 and 100:");
guess = input.nextInt ();
numberOfTries++;

if (guess == numberToGuess) {
win = true;
}

else if (guess > numberToGuess) {
System.out.println ("You guessed too low mate!");
}

else if (guess > numberToGuess) {
System.out.println ("You guessed too high mate!") ;

System.out.println("You win!!");
System.out.println("The random number WAS!" + numberToGuess);
System.out.println("It took you a wopping" + numberOfTries + "TRIES!");

}
}
}
}

这是我的代码,当我尝试运行它时,它基本上卡在这里:

欢迎来到 java 博士。工作目录是 U:\Downloads运行猜谜游戏

如何解决这个问题,以便我可以正常运行程序而不会出现上述错误?

最佳答案

我发现两个错误。

while (win == false); {

应该是

while (win == false) {

(或 while(!win) {),因为分号创建一个空循环体。并且

  else if (guess > numberToGuess) {
System.out.println ("You guessed too low mate!");
}

应该是

  else if (guess < numberToGuess) {
System.out.println ("You guessed too low mate!");
}

或者你有两个更高的测试(这种方式是有意义的)。

关于java - 我点击 "run"它继续加载,没有运行或停止的迹象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26656160/

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