我该如何做到这一点,一旦用户输入一个数字并按下回车(或其他东西)它就会运行if
else
语句?
public static void main(String[] args) {
System.out.println("please guess the number between 1 and 100.");
boolean run = true;
int y = 0;
Random ran = new Random();
int x = ran.nextInt(99);
while (run == true) {
Scanner scan = new Scanner(System.in).useDelimiter(" ");
// System.out.println(scan.nextInt());
y = scan.nextInt();
/*
* if(y > 0){ run = false; } else{ run = true; }
*/
if (y > x) {
System.out.println("Lower...");
} else if (y < x) {
System.out.println("Higher...");
} else if (y == x) {
System.out.println("Correct!");
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
}
}
}