当前位置:网站首页>每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。
每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。
2022-07-28 03:06:00 【北柠陌语】
题目:实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。
解题关键:随机数范围的生成;红球是不重复的随机数;可以购买多注彩票
思路:1)编写需要买的彩票数量
2)设置红球的数组,数组长度为6
3)随机生成红球的数字(用循环)
4)判断随机生成的数字是否和之前的数字重复
5)若与之前的元素有重复的为flase
6)输出flag为true的红球数字
7)设置蓝球的数组
过程: 接下来我们根据我们的解题思路来一步步写代码
1)编写需要买的彩票数量
System.out.println("请输入您需要买的彩票注数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println("您买了" + num + "注彩票");
2)设置红球的数组,数组长度为6
int[] redNum = new int[6];
3)随机生成红球的数字(用循环)
for (int i = 1; i <= num; i++) {
for (int j = 0; j < redNum.length; j++) {
boolean flag = true;//假设此时的元素都没ture
int redNum1 = (int) (Math.random() * 36 + 1);
redNum[j] = redNum1;
4)判断随机生成的数字是否和之前的数字重复
for (int k = 0; k < j; k++) {
if(redNum1 == redNum[k]){
5)若与之前的元素有重复的为flase
flag = false;
j -=1;//j自减1,这样的话这个重复的数字会重新输出一次
break;
}
}
6)输出flag为true的红球数字
if(flag){
System.out.print(redNum[j] + " ");
}
7)设置蓝球的数组
int blueNum = (int) (Math.random() * 15 + 1);
System.out.print(blueNum);
System.out.println();
完整结果如下:

总结:
这个题目用到的知识点有随机数的生成,数组的赋值,还有标记,其中比较重要的是标记
标记(代码中红色的部分):假设该变量都为true,当满足某个条件是,该变量改为false,最后用if判断输出满足条件为true或flase的变量值.
为了方便大家使用,下面附上源码:
//1)编写需要买的彩票数量
System.out.println("请输入您需要买的彩票注数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println("您买了" + num + "注彩票");
//2)设置红球的数组,数组长度为6
int[] redNum = new int[6];
//3)随机生成红球的数字(用循环)
for (int i = 1; i <= num; i++) {
for (int j = 0; j < redNum.length; j++) {
boolean flag = true;//假设此时的元素都没ture
int redNum1 = (int) (Math.random() * 36 + 1);
redNum[j] = redNum1;
//4)判断随机生成的数字是否和之前的数字重复
for (int k = 0; k < j; k++) {
if(redNum1 == redNum[k]){
//5)若与之前的元素有重复的为flase
flag = false;
j -=1;//j自减1,这样的话这个重复的数字会重新输出一次
break;
}
}
//6)输出flag为true的红球数字
if(flag){
System.out.print(redNum[j] + " ");
}
}
//7)设置蓝球的数组
int blueNum = (int) (Math.random() * 15 + 1);
System.out.print(blueNum);
System.out.println();
}明日练习: 输出如下图片的内容

大家可以自己写写,明天12点我准时发出我的写法哦,明天12点不见不散
边栏推荐
猜你喜欢

Response to questions about the balanced beacon group of Hubei University of Arts and Sciences

How does win11 display fixed applications?

关于湖北文理学院平衡信标组的疑问回应

mysql存储过程 使用游标实现两张表数据同步数据

Original title of Blue Bridge Cup

Redis内存回收

STM32 RT-Thread虚拟文件系统挂载操作

Shell writing specifications and variables

静态博客搭建工具汇总

Engineering Geology Practice - engineering geology problem set
随机推荐
Decision tree and random forest learning notes (1)
ThreadLocal使用场景
Exness: Japanese prices rose and incomes fell, with the pound / yen breaking 165
Leetcode 208. implement trie (prefix tree) (2022.07.27)
版本兼容的问题
Shell: resource monitoring script and high load alarm
Pytoch correlation gradient echo
Summary of concurrent programming interview questions
What are the fragments of MySQL
叶子识别 颜色的特征提取 缺陷检测等
如何一键进行重装Win11系统
响应式高端网站模板源码图库素材资源下载平台源码
VI command details
机器人开发--丝杠与导轨
RBD块存储设备的扩容以及缩容操作(六)
IO analog serial port of stm32
什么是虚函数?
Win11怎么显示固定应用?
Softek Barcode Reader 9.1.5
Robot development -- lead screw and guide rail