import java.util.Random;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
System.out.println("猜字游戏开始了!");
System.out.println("请输入1-100之间的数字!");
//创建Random类变量
Random ran = new Random();
//变量.使用功能next()获取1-100随机数
int ranNumber = ran.nextInt(100)+1;
//创建Scanner类变量
Scanner sc = new Scanner(System.in);
while (true) {
//获取键盘输入
int number = sc.nextInt();
//随机数和用户输入数字进行比较
if(number > ranNumber) {
System.out.println("猜大了");
}
else if(number < ranNumber) {
System.out.println("猜小了");
}
else{
System.out.println("猜中了");
break;
}
}
}
}
注:适合初学者理解Random类、Scanner类、死循环、if-else循环