File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22 * @由于个人水平有限, 难免有些错误, 还请指点:
33 * @Author: cpu_code
44 * @Date: 2020-09-12 14:19:23
5- * @LastEditTime: 2020-09-12 14:19:54
6- * @FilePath: \java\javaAPI\print_random.java
5+ * @LastEditTime: 2020-09-13 14:44:11
6+ * @FilePath: \java\javaAPI\Random\ print_random.java
77 * @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
88 * @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
99 * @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
1010 * @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
1111 */
12- package javaAPI ;
12+ package javaAPI . Random ;
1313
1414import java .util .Random ;
1515
16+ /*
17+ Random类用来生成随机数字。使用起来也是三个步骤:
18+
19+ 1. 导包
20+ import java.util.Random;
21+
22+ 2. 创建
23+ Random r = new Random(); // 小括号当中留空即可
24+
25+ 3. 使用
26+ 获取一个随机的int数字(范围是int所有范围,有正负两种):int num = r.nextInt()
27+ 获取一个随机的int数字(参数代表了范围,左闭右开区间):int num = r.nextInt(3)
28+ 实际上代表的含义是:[0,3),也就是0~2
29+ */
30+
1631public class print_random {
1732 public static void main (String [] args ) {
18- // 创建键盘录入数据的对象
1933 Random r = new Random ();
2034
21- for (int i = 0 ; i < 3 ; i ++) {
22- // 随机生成一个数据
23- // 范围在 0 (包括)和 指定值 n (不包括)之间的 int 值
24- int number = r .nextInt (10 );
25- // 输出数据
26- System .out .println ("number:" + number );
27- }
35+ int num = r .nextInt ();
36+
37+ System .out .println ("随机数 :" + num );
2838 }
2939}
3040
3141/*
32- number:8
33- number:6
34- number:3
42+ 随机数 :-145013416
3543*/
You can’t perform that action at this time.
0 commit comments