Random类 用来生成随机数字;
使用起来三个步骤
1、导包 手册中看包
2、创建 文档中看构造方法摘要
3、使用 文档中看方法摘要
package cn.api;
/*
1、导包 手册中看包
2、创建 文档中看构造方法
3、使用 文档中看成员摘要
*/
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 100; i++) {
//随机生成一个int范围内的函数
int num = r.nextInt();
System.out.println(num);
}
}
}
随机生成0-8的int数字
package cn.api;
import java.util.Random;
public class RandomDemo {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 100; i++) {
int num = r.nextInt(9); //随机生成0-8的数字
System.out.println(num);
}
}
}
要求:获取1-10的int数字;
问题分析:方法参数的结果是从0 到 N-1的随机结果;
解决办法随机数成后+1即可;
imp