import java.util.Random;import java.util.Scanner;public class RandomGame {
public static void main(String[] args) {
Random r = new Random();
int randomNum = r.nextInt(100);
int i = 5;
while(true) {
System.out.print("请输入数字(0-99),你还有" + i + "次机会:");
int guess = new Scanner(System.in).nextInt();
if(guess > randomNum) {
System.out.println("大了");
}else if(guess < randomNum) {
System.out.println("小了");
}else {
System.out.println("恭喜你猜中了!");
break;
}
if(i == 1) {
System.out.println("GameOver");
break;
} i--;
}
}
}
|
|