- 使用Java基础的Random类写的一个小游戏大家感兴趣可以看一看
package com.zhou;
import java.util.Random;
import java.util.Scanner;
public class day0818 {
public static void main(String[] args) {
guessNum();
}
public static void guessNum() {
Random rd = new Random();
Scanner sc = new Scanner(System.in);
int number = rd.nextInt(100)+1;
int count = 1;
while (true) {
System.out.println("请输入你要猜的数字");
System.out.println("===================");
int guessNum = sc.nextInt();
if (guessNum>number) {
System.out.println("你猜的数字太大了");
System.out.println("===================");
}else if (guessNum<number) {
System.out.println("你猜的数字太小了");
System.out.println("===================");
}else {
System.out.println("恭喜你,只用了"+count+"次就答对了!!!真棒!");
break;
}
count++;
}
}
}