自学Random类,然后实现获取一个1-100之间的随机数
A: int num = (int)(Math.random()*100)+1
B: Random r = new Random();
int num = r.nextInt(100)+1;
import java.util.Random;
class RandomDemo
{
public static void main(String[] args)
{
//创建对象
//Random r = new Random();
Random r = new Random(17);
//调用功能
//System.out.println(r.nextInt());
/*
for(int x=0; x<1000; x++)
{
System.out.println(r.nextInt(100));
}
*/
for(int x=0; x<10; x++)
{
int num = r.nextInt(100);
System.out.println(num);
}
}
} |
|