当前位置:网站首页>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
边栏推荐
- Raspberry pie development relay control lamp
- 嵌入式数据库--SQLite
- 颜色的识别方法和探索 基于matlab
- 过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?
- xctf攻防世界 Web高手进阶区 unserialize3
- How to uninstall clean ZABBIX service? (super detailed)
- 8000字讲透OBSA原理与应用实践
- 203. Remove linked list elements
- ASEMI整流桥GBPC3510在直流有刷电机中的妙用
- Shell: resource monitoring script and high load alarm
猜你喜欢

Shell writing specifications and variables

tensorboard使用记录

Analysis of redis network model

IO analog serial port of stm32

Summary of static blog building tools

VMware virtual machine network settings

How to make the Internet access the intranet IP (used by esp8266 web pages)

IronOCR for .NET 2022.8

【SAML SSO解决方案】上海道宁为您带来SAML for ASP.NET/SAML for ASP.NET Core下载、试用、教程

每周推荐短视频:如何正确理解“精益”这个词?
随机推荐
LabVIEW加载和使用树型控件项目中的定制符号
ssm整合(整合配置)
什么是虚函数?
How to reinstall win11 system with one click
[nature of class (in Objective-C language)]
How to arrange PCB screen printing? Please check this manual!
695. 岛屿的最大面积
Redis基本操作
LeetCode 第二十九天
Win11怎么显示固定应用?
C -- switch case statement
Win11输入法的选字框不见了怎么办?
The open source of "avoiding disease and avoiding medicine" will not go far
2022-07-27:小红拿到了一个长度为N的数组arr,她准备只进行一次修改, 可以将数组中任意一个数arr[i],修改为不大于P的正数(修改后的数必须和原数不同), 并使得所有数之和为X的倍数。
C # set TextBox control not editable
Win11如何重命名音频设备
What are the fragments of MySQL
VI command details
golang 获取循环嵌套结构的tag
ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小