当前位置:网站首页>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
边栏推荐
猜你喜欢

Malloc, free, calloc, realloc dynamic memory development functions in dynamic memory management

每日练习------实现双色球的彩票功能。规则:从36个红球中随机选择不重复的6个数,从15个篮球中随机选择1个组成一注彩票。可以选择买多注。

Summary of concurrent programming interview questions

Redis basic operation

max_ pool2d(): argument ‘input‘ (position 1) must be Tensor, not NoneType

Uniapp - make phone calls and send text messages

8000字讲透OBSA原理与应用实践

53. Maximum subarray maximum subarray sum

IronOCR for .NET 2022.8

max_pool2d(): argument ‘input‘ (position 1) must be Tensor, not NoneType
随机推荐
C # set TextBox control not editable
Digital twin technology drives smart factory to reduce burden and enhance operation and maintenance benefits
动画(animation)
Redis 5 kinds of data structure analysis
Robot development -- lead screw and guide rail
ES6 从入门到精通 # 07:解构赋值
Billions of asset addresses are blacklisted? How to use the tether address freezing function?
xctf攻防世界 Web高手进阶区 PHP2
某宝模拟登录,减少二次验证的方法
VMware virtual machine network settings
如何卸载干净zabbix服务?(超详细)
Analysis of redis network model
[R language] environment specifies to delete RM function
Detailed tutorial of one click reinstallation of win7 system
Version compatibility issues
20220727 use the Bluetooth module hc-05 of Huicheng technology to pair mobile phones for Bluetooth serial port demonstration
SSM integration (integrated configuration)
过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?
When QML uses layout layout, a large number of < unknown file >: QML qquicklayoutattached: binding loop detected for property circular binding warnings appear
203.移除链表元素