当前位置:网站首页>Daily practice ----- realize the lottery function of two-color ball. Rules: Six non repeating numbers are randomly selected from 36 red balls, and one from 15 basketball is randomly selected to form a
Daily practice ----- realize the lottery function of two-color ball. Rules: Six non repeating numbers are randomly selected from 36 red balls, and one from 15 basketball is randomly selected to form a
2022-07-28 03:33:00 【Beiningmo language】
subject : Realize the lottery function of two-color ball . The rules : from 36 Randomly choose the non repeating one among the red balls 6 Number , from 15 Randomly choose from basketball 1 Make up a lottery . You can choose to buy more notes .
The key to solving the problem is : Generation of random number range ; Red balls are random numbers that do not repeat ; You can buy more lottery tickets
Ideas :1) Write the number of tickets you need to buy
2) Set the array of red balls , The array length is 6
3) Numbers that randomly generate red balls ( Use the cycle )
4) Judge whether the randomly generated number duplicates the previous number
5) If there is a repetition with the previous element, it is flase
6) Output flag by true Red Globe number of
7) Set the array of basketball
The process : Next, we write code step by step according to our problem-solving ideas
1) Write the number of tickets you need to buy
System.out.println(" Please enter the number of lottery tickets you need to buy :");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(" Did you buy it " + num + " Note lottery ");
2) Set the array of red balls , The array length is 6
int[] redNum = new int[6];
3) Numbers that randomly generate red balls ( Use the cycle )
for (int i = 1; i <= num; i++) {
for (int j = 0; j < redNum.length; j++) {
boolean flag = true;// Suppose there are no elements at this time ture
int redNum1 = (int) (Math.random() * 36 + 1);
redNum[j] = redNum1;
4) Judge whether the randomly generated number duplicates the previous number
for (int k = 0; k < j; k++) {
if(redNum1 == redNum[k]){
5) If there is a repetition with the previous element, it is flase
flag = false;
j -=1;//j Self reduction 1, In this case, the repeated number will be output again
break;
}
}
6) Output flag by true Red Globe number of
if(flag){
System.out.print(redNum[j] + " ");
}
7) Set the array of basketball
int blueNum = (int) (Math.random() * 15 + 1);
System.out.print(blueNum);
System.out.println();
The complete results are as follows :

summary :
The knowledge points used in this topic are the generation of random numbers , The assignment of an array , And the mark , The more important one is the mark
Mark ( The red part of the code ): Assume that the variables are true, When a certain condition is met , This variable is changed to false, Last use if Judge that the output meets the condition of true or flase Variable value .
For the convenience of everyone , The source code is attached below :
//1) Write the number of tickets you need to buy
System.out.println(" Please enter the number of lottery tickets you need to buy :");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(" Did you buy it " + num + " Note lottery ");
//2) Set the array of red balls , The array length is 6
int[] redNum = new int[6];
//3) Numbers that randomly generate red balls ( Use the cycle )
for (int i = 1; i <= num; i++) {
for (int j = 0; j < redNum.length; j++) {
boolean flag = true;// Suppose there are no elements at this time ture
int redNum1 = (int) (Math.random() * 36 + 1);
redNum[j] = redNum1;
//4) Judge whether the randomly generated number duplicates the previous number
for (int k = 0; k < j; k++) {
if(redNum1 == redNum[k]){
//5) If there is a repetition with the previous element, it is flase
flag = false;
j -=1;//j Self reduction 1, In this case, the repeated number will be output again
break;
}
}
//6) Output flag by true Red Globe number of
if(flag){
System.out.print(redNum[j] + " ");
}
}
//7) Set the array of basketball
int blueNum = (int) (Math.random() * 15 + 1);
System.out.print(blueNum);
System.out.println();
}Practice tomorrow : Output the contents of the following pictures

You can write by yourself , Tomorrow, 12 I'll send my writing on time , Tomorrow, 12 I'll see you when I see you
边栏推荐
- 4天Excel实战训练营,0.01元特惠仅三天,赠200套学习资料包
- Animation
- 自定义注解的使用
- ThreadLocal usage scenario
- ssm整合(整合配置)
- The wonderful use of asemi rectifier bridge GBPC3510 in DC brush motor
- 20220727使用汇承科技的蓝牙模块HC-05配对手机进行蓝牙串口的演示
- C语言实现动态版本的通讯录
- Redis source code analysis (who says C language can't analyze it?)
- 2022最新Android Handler相关面试题总结
猜你喜欢

Redis实现分布式锁

C WinForm development: how to add pictures to project resources

机器人开发--丝杠与导轨

Shell: resource monitoring script and high load alarm
![[download file] uniapp develops small programs, downloads files and saves them locally](/img/0f/4963178e44f58ad6e3097844bb6ffd.png)
[download file] uniapp develops small programs, downloads files and saves them locally
![2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb](/img/24/fbf63272f83b932e0ee953f8d4db75.png)
2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb

叶子识别 颜色的特征提取 缺陷检测等

Defect detection of BP SVM system design of leaf defect detection

How to uninstall clean ZABBIX service? (super detailed)

ASEMI整流桥GBPC3510在直流有刷电机中的妙用
随机推荐
数字经济已成为最大看点
xctf攻防世界 Web高手进阶区 unserialize3
Shell:一键部署pxe
Redis persistence mechanism
4天Excel实战训练营,0.01元特惠仅三天,赠200套学习资料包
容器相关的概念
Redis memory recycling
如何使用JDBC操作数据库
Win11怎么显示固定应用?
ThreadLocal usage scenario
Outlook tutorial, how to use color categories and reminders in outlook?
[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
Win11 how to rename an audio device
695. 岛屿的最大面积
同时导出多个excel,并且一个excel中包含多个sheet
Embedded database -- SQLite
Digital economy has become the biggest attraction
"Introduction to engineering electromagnetic field" after class exercises with answers
Robot development -- lead screw and guide rail
redis源码分析(谁说C语言就不能分析了?)