day07-random类的使用与练习

This commit is contained in:
2025-12-29 11:39:17 +08:00
parent 268fa720bf
commit cc9b5436b5
3 changed files with 58 additions and 0 deletions

View 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);
}
}
}

View 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);
}
}
}

View 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 {
}