random 作用:产生随机数
导包
import java.util.Random;
创建对象
Random r = new Random();
获取随机数
int number = r.nextInt(10); //获取数据范围(0,10)不包括10
import java.util.Random;
public class hello {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i<10; i++){
int number = r.nextInt(10);
System.out.println("number:" + number);
}
int x = r.nextInt(100) +1;
System.out.println(x);
}
}
import java.util.Random;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
Random r = new Random();
int number = r.nextInt(100) + 1;
while(true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字:");
int guessNumber = sc.nextInt();
if(guessNumber > number) {
System.out.println("您输入的数字大了");
}else if(guessNumber<number){
System.out.println("您输入的数字小了");
}else{
System.out.println("恭喜你");
break;
}
}
}
}