4 回答
TA贡献1725条经验 获得超7个赞
希望这是你期待的逻辑......
public class GuessingGame {
public static void main(String[] args) {
int random;
random = (int) (Math.random() * 10) + 1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter you guess between 1 to 10");
int guess = sc.nextInt();
while (guess != 0) {
if (guess < random) {
System.out.println("You guessed too low!"+random);
System.out.println("Enter you guess between 1 to 10");
guess = sc.nextInt();
continue;
} else if (guess > random) {
System.out.println("You guessed too High"+random);
System.out.println("Enter you guess between 1 to 10");
guess = sc.nextInt();
continue;
}else{
if (guess == random) {
System.out.println("You guessed it right, the number is " + random);
}
break;
}
}
}
}
TA贡献1831条经验 获得超10个赞
像这样尝试
import java.util.Scanner;
public class GuessingGame {
int random;
public GuessingGame() {
generateNumber();
guess();
}
public void generateNumber() {
// The following lines generate and output a random number between 1 and
// 10
random = (int) (Math.random() * 10) + 1;
}
// Write the guess() method below
public void guess() {
// Use scanner to accept a user input
// Create a new scanner object to receive user input
Scanner sc = new Scanner(System.in);
System.out.println("Enter you guess between 1 to 10");
int guess = sc.nextInt();
while (guess != random) {
// write your code below
if (guess < random) {
System.out.println("You guessed too low!");
} else {
System.out.println("You guessed too high");
}
guess = sc.nextInt();
}
System.out.println("You guessed it right, the number is " + random);
}
public static void main(String[] args) {
new GuessingGame();
}
}
TA贡献1865条经验 获得超7个赞
在 java 中,continue 一词仅在循环内使用,并在不考虑 continue 语句下面的代码的情况下开始循环中的下一次迭代。你这里的错误是你根本没有使用循环,所以 java 编译器不知道如何处理 continue 语句。
我假设您在完成后继续退出 if 语句。如果是这样,那么您可以完全删除 continue 语句,它应该会运行。
添加回答
举报