day07-random类的使用与练习
This commit is contained in:
23
day07/src/com/inmind/random02/Demo05.java
Normal file
23
day07/src/com/inmind/random02/Demo05.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind.random02;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/*
|
||||||
|
获取随机数Random类的使用
|
||||||
|
*/
|
||||||
|
public class Demo05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建random对象,调用nextInt方法
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
/*for (int i = 0; i < 100; i++) {
|
||||||
|
int a = random.nextInt();//在int的取值范围内获取一个随机值
|
||||||
|
System.out.println(a);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
int result = random.nextInt(10);//获取0~9的随机数
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
day07/src/com/inmind/random02/Demo06.java
Normal file
18
day07/src/com/inmind/random02/Demo06.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package com.inmind.random02;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/*
|
||||||
|
获取10个55~66之间的值,(包含55和66)
|
||||||
|
|
||||||
|
random.nextInt(10);//获取0~9的随机数
|
||||||
|
*/
|
||||||
|
public class Demo06 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Random random = new Random();
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
int result = random.nextInt(12)+55;//0~9: 55~66 -55 -->0~11--->12
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
day07/src/com/inmind/random02/Demo07.java
Normal file
17
day07/src/com/inmind/random02/Demo07.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.inmind.random02;
|
||||||
|
/*
|
||||||
|
常用类_Random_猜数字游戏(1~100)
|
||||||
|
猜数字小游戏
|
||||||
|
游戏开始时,会随机生成一个1-100之间的整数number 。
|
||||||
|
玩家猜测一个数字guessNumber ,会与number 作比较,系统提示大了或者小了,直到玩家猜中,游戏结束。
|
||||||
|
|
||||||
|
分析:
|
||||||
|
1.使用random类获取一个随机值(1~100)
|
||||||
|
2.使用scanner输入你所猜的值,假设输入的是33
|
||||||
|
(死循环,while(true))
|
||||||
|
a.将输入的猜的值跟随机数做比较,如果猜的值大了,需要提示猜的值:33大了
|
||||||
|
b.将输入的猜的值跟随机数做比较,如果猜的值小了,需要提示猜的值:33小了
|
||||||
|
c.将输入的猜的值跟随机数做比较,如果猜的值一致,需要提示正确,并结束游戏
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user