我是java的新手,我有一个我在学校工作的项目。当我运行程序时,它给了我所要求的一切,除了它打印的事实"数字更大"在答案之上。有什么我想念的吗?感谢
import java.util.Scanner;
import java.util.Random;
public class Project {
public static void main(String[] args) {
{
Scanner reader = new Scanner(System.in);
int numberOfGuesses = 0;
int x = 0;
Random rand = new Random();
int randomNumber = rand.nextInt(100) + 1;
while (x != 1) {
System.out.print("Guess an integer between 1 and 100: " );
int guessedNumber = reader.nextInt();
if (guessedNumber < randomNumber) {
System.out.println("The number is bigger");
}
if (guessedNumber > randomNumber) {
System.out.println("The number is smaller");
}
else{
x++;
}
numberOfGuesses++;
}
System.out.println("You're correct! It took" + " " + numberOfGuesses + " " + "guesses.");
}
}
}
答案 0 :(得分:1)
onSubmit()
要
if (guessedNumber < randomNumber) {
System.out.println("The number is bigger");
}
if (guessedNumber > randomNumber) {
System.out.println("The number is smaller");
}
else{
x++;
}
答案 1 :(得分:0)
当数字越大时,你的第二个如果失败并转到其他部分,它将x递增到1并存在程序。将第二个if语句放在else if。
if (guessedNumber < randomNumber) {
System.out.println("The number is bigger");
}
else if (guessedNumber > randomNumber) {
System.out.println("The number is smaller");
}
else{
x++;
}