当前位置:网站首页>Simulation of two-color ball system to judge the winning situation
Simulation of two-color ball system to judge the winning situation
2022-06-30 15:40:00 【Frog claw white】
Case study

analysis
Analysis on the judgment of winning the prize :
Define a method , Can receive an array of winning numbers , An array of user selected numbers .
Judge the final winning situation according to the number of hit red balls and basketball, and output the details and winning amount .
Code
package com.xxf.casedemo;
import java.util.Random;
import java.util.Scanner;
public class shuangsebog {
public static void main(String[] args) {
// Random 6 A red ball number (1-33 Can't repeat ), Random basketball number (1-16) You can take an array and put it up as the winning number
int[] luckNumber = createLuckNumber();
// Call a method to let the user enter 7 Numbers , Select the number as the user
int[] userNumber = userInputNumber();
judge(luckNumber,userNumber);
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println();// Line break
}
public static void judge(int[] luckNumber, int[] userNumber) {
// Judge whether you win the prize
// Definition 2 Variables store the number of red ball hits respectively , And the number of basketball Hits
int redHitNumber = 0;
int blueHitNumber = 0;
// Judge how many red balls you hit , Start Statistics
for (int i = 0; i < userNumber.length - 1; i++) {
for (int j = 0; j < luckNumber.length - 1; j++) {
// Every time we find the equality , It means that the current number hit
if (userNumber == luckNumber) {
redHitNumber++;
break;
}
}
}
// Blue ball number hit
blueHitNumber = luckNumber[6] == userNumber[6] ? 1 : 0;
System.out.println(" The winning number is :");
printArray(luckNumber);
System.out.println(" Betting number is :");
printArray(userNumber);
System.out.println(" How many red balls did you hit :"+redHitNumber);
System.out.println(" Whether you hit the basketball :"+(blueHitNumber==1?" yes ":" no "));
// Judge the winning situation
if (blueHitNumber==1&&redHitNumber<3){
System.out.println(" Congratulations , It's in 5 element !");
}else if(blueHitNumber==1&&redHitNumber==3||blueHitNumber==0&&redHitNumber==4){
System.out.println(" Congratulations , It's in 10 element !");
}else if(blueHitNumber==1&&redHitNumber==4||blueHitNumber==0&&redHitNumber==5) {
System.out.println(" Congratulations , It's in 200 element !");
}else if(blueHitNumber==1&&redHitNumber==5) {
System.out.println(" Congratulations , It's in 3000 element !");
}else if(blueHitNumber==0&&redHitNumber==6) {
System.out.println(" Congratulations , It's in 500 Ten thousand yuan !");
}else if(blueHitNumber==0&&redHitNumber==6) {
System.out.println(" Congratulations , It's in 1000 Ten thousand yuan !");
}
}
public static int[] createLuckNumber() {
// Define a dynamically initialized array , Storage 7 A digital
int[] numbers = new int[7];
// Traversal array , Generate a corresponding number for each location
Random r = new Random();
for (int i = 0; i < numbers.length - 1; i++) {
while (true) {
int data = r.nextInt(33) + 1;
boolean flag = true;
for (int j = 0; j < i; j++) {
if (numbers[j] == data) {
flag = false;
break;
}
}
if (flag) {
numbers[i] = data;
break;
}
}
}
numbers[numbers.length - 1] = r.nextInt(16) + 1;
return numbers;
}
public static int[] userInputNumber() {
// Define an array to store 7 Numbers
int[] numbers = new int[7];
// Let the user type 6 A red ball number
Scanner sc = new Scanner(System.in);
for (int i = 0; i < numbers.length - 1; i++) {
System.out.println(" Please enter the number " + (i + 1) + " A red ball number (1-33, Do not repeat ):");
int data = sc.nextInt();
// Save the currently entered data into the array
numbers[i] = data;
}
// Enter a basketball number separately
System.out.println(" Please enter the basketball number (1-16):");
numbers[6] = sc.nextInt();
return numbers;
}
}
test result

边栏推荐
- Voice codec based on machine learning Agora silver: support high quality voice interaction at ultra-low bit rate
- 1151 LCA in a binary tree (30 points)
- G - building a space station
- [ten thousand words long article] thoroughly understand load balancing
- Introduction to using 51 single chip microcomputer to control steering gear
- Openresty built in variable
- Soap comparison result file description
- Help you accumulate audio and video knowledge, Agora developer's roaming guide officially set sail
- Complement (Niuke)
- Database connection to company database denied
猜你喜欢

Database connection to company database denied

J - Borg maze (minimum spanning tree +bfs)

开源 STM32 USB-CAN项目

比亚迪越来越像华为?

Tetris source code (color version)

Anyrtc implements application scenarios based on webrtc

4.2 escape characters

Start your global dynamic acceleration journey of Web services in three steps

【Leetcode】链表排序(逐步提高时空复杂度)

Teach you a learning method to quickly master knowledge
随机推荐
The short video and live broadcast incubation training camp with goods opens nationwide enrollment!
Policy Center > Malware > Malware
Policy Center > Deceptive Behavior
Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.
Flask Sqlalchemy - how to use custom query criteria ---orm (7)
B. Moamen and k-subarrays (codeforce+ binary search)
How should we understand the variability of architecture design?
G - building a space station
Advanced functions of ES6 operation array map (), filter (), reduce()
The crystal ball "data insight" was officially launched: insight into the change of consumption trend and the details of interactive experience
Message queue ten questions
各省GDP可视化案列,附带csv Metabase处理
Imitating freecodecamp to build a programming ability test platform
4.4 string
Policy Center-User Data
剑指 Offer II 080. 含有 k 个元素的组合 回溯
Openresty built in variable
The sound network has fully opened the real-time transmission network sd-rtn, which has been free of network wide accidents for seven years - this is FPA!
Help you accumulate audio and video knowledge, Agora developer's roaming guide officially set sail
1018 public bike Management (30 points)