| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import java.util.Arrays;
- import java.util.Random;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo04
- * @description
- * @create 2026/7/17
- */
- public class Demo04 {
- /*
- 课堂练习题:
- 1、生成6位随机验证码
- 定义一个方法 getVerificationCode(),生成并返回 100000~999999 之间 的随机数,作为6位数字验证码
- 每次运行结果都要不同。
- 效果:
- 调用getVerificationCode()方法
- 返回:8795326
- 2、生成随机幸运数字
- 定义一个方法getLuckyNumber(),生成并返回一个1~100之间的随机整数作为幸运数字
- 3、抛骰子
- 定义一个方法diceRoll(),模拟抛一颗六面骰子,返回1~6之间的随机数。
- 4、生成随机颜色 RGB
- 定义一个方法getRandomColor() ,生成并返回一个包含3个元素的int类型数组,分别表达RGB的三个颜色色值。
- 每个色值范围 0~255
- 效果:
- 调用getRandomColor()方法
- 返回:[128,4,6]
- */
- public static int[] getRandomColor(){
- //1、创建存储颜色值的数组,长度为3
- int[] color=new int[3];
- //2、创建Random 对象
- Random ran=new Random();
- color[0]=ran.nextInt(256);
- color[1]=ran.nextInt(256);
- color[2]=ran.nextInt(256);
- return color;
- }
- public static void main(String[] args) {
- int[] color=getRandomColor();
- System.out.println(Arrays.toString(color));
- }
- }
|