猜数字游戏
public static void main(String[] args) { System.out.println("猜数字游戏开始啦!"); System.out.println("请输入1-100之间的数字"); Random ran = new Random();//创建一个随机数生成器 int ranNum = ran.nextInt(100);//生成[0,100)之间的随机数 @SuppressWarnings("resource") Scanner sc = new Scanner(System.in);//创建一个扫描器 while(true) { int inputNum = sc.nextInt();//从键盘输入获取值 if(ranNum>inputNum) { System.out.println("数字小了"); }else if(ranNum<inputNum) { System.out.println("数字大了"); }else { System.out.println("猜对了"); break; } }
}