我只是玩弄java,想做一个简单的程序,用户在哪里;我必须猜测/输入正确的数字,直到它是正确的。我该怎么做,程序可以继续运行,打印出“再次猜测”,直到用户输入正确的数字。也许是布尔?我不确定。这是我迄今为止所拥有的。简单的Java猜测程序。继续
import java.util.Scanner;
public class iftothemax {
public static void main(String[] args) {
int myInt = 2;
// Create Scanner object
Scanner input = new Scanner(System.in);
//Output the prompt
System.out.println("Enter a number:");
//Wait for the user to enter a number
int value = input.nextInt();
if(value == myInt) {
System.out.println("You discover me!");
}
else {
//Tell them to keep guessing
System.out.println("Not yet! You entered:" + value + " Make another guess");
input.nextInt();
}
}
使用while循环,循环,直到值是正确的 – SomeJavaGuy