Demo04.java 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import java.util.Arrays;
  2. import java.util.Random;
  3. /**
  4. * @author WanJl
  5. * @version 1.0
  6. * @title Demo04
  7. * @description
  8. * @create 2026/7/17
  9. */
  10. public class Demo04 {
  11. /*
  12. 课堂练习题:
  13. 1、生成6位随机验证码
  14. 定义一个方法 getVerificationCode(),生成并返回 100000~999999 之间 的随机数,作为6位数字验证码
  15. 每次运行结果都要不同。
  16. 效果:
  17. 调用getVerificationCode()方法
  18. 返回:8795326
  19. 2、生成随机幸运数字
  20. 定义一个方法getLuckyNumber(),生成并返回一个1~100之间的随机整数作为幸运数字
  21. 3、抛骰子
  22. 定义一个方法diceRoll(),模拟抛一颗六面骰子,返回1~6之间的随机数。
  23. 4、生成随机颜色 RGB
  24. 定义一个方法getRandomColor() ,生成并返回一个包含3个元素的int类型数组,分别表达RGB的三个颜色色值。
  25. 每个色值范围 0~255
  26. 效果:
  27. 调用getRandomColor()方法
  28. 返回:[128,4,6]
  29. */
  30. public static int[] getRandomColor(){
  31. //1、创建存储颜色值的数组,长度为3
  32. int[] color=new int[3];
  33. //2、创建Random 对象
  34. Random ran=new Random();
  35. color[0]=ran.nextInt(256);
  36. color[1]=ran.nextInt(256);
  37. color[2]=ran.nextInt(256);
  38. return color;
  39. }
  40. public static void main(String[] args) {
  41. int[] color=getRandomColor();
  42. System.out.println(Arrays.toString(color));
  43. }
  44. }