Topic introduction
1. Introduction to playing method
Each betting number of "two-color ball" consists of 6 red ball numbers and 1 blue ball number. The red ball number is selected from 1 ~ 33, and the blue ball number is selected from 1 ~ 16. The number of matches and the color of the ball determine whether or not to win the prize.
2. Winning rules
3. Requirements
1. Generate the winning number of the current two-color ball. (Note: 1. The generated red ball is random, orderly and non repeatable. 2. The random range of blue ball and red ball is different, and basketball is allowed to repeat with red ball)
2. There are two ways for users to generate data
(1) Automatically generated. (machine selected)
(2) Enter the quiz number through the console. (optional)
3. Record the number of correct balls in red ball and blue ball guessing, and output the guessing results and the current two-color ball number according to the award conditions.
analysis
- The array is used to represent the selection number of two-color ball. The size of the array is 7. The first six elements represent the number of red ball and the last element represents the number of blue ball.
int[] array = new int[7]; // Store the number of two-color ball. The first 6 are red balls and the last is blue ball
Two arrays are defined here. One represents the user's two-color ball number and the other represents the winning two-color ball number.
int[] array = new int[7]; // Store the number of two-color ball. The first 6 are red balls and the last is blue ball int[] result; // The winning two-color ball number
- Define two counters to record the number of red ball and blue ball winners respectively.
int countRed = 0; // Number of red ball winners int countBlue = 0; // How many pieces did the blue ball win
- switch is used for branch operation to realize machine selection and self selection
- The machine selected numbers and winning numbers are automatically generated by random numbers. Here, some repetitive contents are written in a function.
// Random generation of two-color spheres public static int[] randomArray() { int[] array = new int[7]; // Generate 6 different random numbers to represent 6 red balls for (int i = 0; i < 6; i++) { boolean flag = true; // Define a flag to judge whether the red ball number is repeated int randomNum = (int)(Math.random() * 33 + 1); // Randomly generate a red ball number (1 ~ 33) // Only the red ball requires different numbers to traverse the array, so you only need to traverse the first 6 numbers for (int j = 0; j < 6; j++) { if (randomNum == array[j]) { // Compare all the numbers in the array with the newly generated random numbers i--; // Roll i back flag = false; // Indicates repetition break; } } if (flag) { // If not repeated array[i] = randomNum; // Store the randomly generated red ball number in the array } } array[6] = (int)(Math.random() * 16 + 1); // Randomly generate basketball numbers 1 ~ 16 return array; }
- Red balls need to be sorted in ascending order, so you only need to sort the first 6 elements (a total of 7 elements) in the array. Bubble sorting is used here.
// Sort (from small to large) bubble public static int[] sort (int[] a) { // Only the first 6 numbers are sorted, so it is a.length-2 here for (int i = 0; i < a.length-2; i++) { for (int j = 0; j < a.length-i-2; j++) { if (a[j] > a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } return a; }
Not much code.
source code
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("============Welcome to the two-color ball machine selection system (bonus of 5 million in this period)============"); System.out.println("1: Machine selected two-color ball\t\t\t2: Optional two-color ball"); int n = sc.nextInt(); int[] array = new int[7]; // Store the number of two-color ball. The first 6 are red balls and the last is blue ball int[] result; // The winning two-color ball number int countRed = 0; // Number of red ball winners int countBlue = 0; // Number of blue ball winners switch(n) { case 1: // Machine selected two-color ball // Randomly generated two-color ball number array = randomArray(); // Sort the generated red ball numbers sort(array); // Output machine selection results System.out.print("Machine selected red ball number:["); for (int i = 0; i < 6; i++) { if (i != 5) { System.out.print(array[i] + ", "); } else { System.out.println(array[i] + "]"); } } System.out.println("Machine selected basketball number:[" + array[6] + "]"); break; case 2: // Optional two-color ball System.out.println("Optional"); System.out.println("Please enter the red ball number you selected (1)~33)"); for (int i = 0; i < 6; i++) { System.out.print("Please enter page" + (i+1) + "Number of red balls:"); array[i] = sc.nextInt(); } System.out.print("Please enter the number of the blue ball (1~16): "); array[6] = sc.nextInt(); // Red ball sorting sort(array); break; default: System.out.println("Input error!"); System.exit(0); } System.out.println("========================The prize is being opened. Please wait========================"); // Randomly generate winning numbers result = randomArray(); // Sort the winning numbers of the generated red ball sort(result); // Output the winning number System.out.print("Red ball number of this issue:["); for (int i = 0; i < 6; i++) { if (i != 5) { System.out.print(result[i] + ", "); } else { System.out.println(result[i] + "]"); } } System.out.println("Current winning basketball number:[" + result[6] + "]"); // Count the number of red balls for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) { if (array[i] == result[j]) { countRed++; } } } if (array[6] == result[6]) { // Count the number of blue balls countBlue++; } System.out.println("Choose 6+1 in"+ countRed + "+" + countBlue); // Judge whether to win the prize if (countRed <= 3 && countBlue == 0) { System.out.println("I'm sorry you didn't win the prize ╭(╯^╰)╮"); } else if (countRed < 3 && countBlue == 1) { System.out.println("Congratulations on winning the sixth prize! Five dollars!"); } else if ((countRed ==3 && countBlue == 1) || (countRed == 4 && countBlue == 0)) { System.out.println("Congratulations on winning the fifth prize! Ten dollars!!"); } else if ((countRed == 4 && countBlue == 1) || (countRed == 5 && countBlue == 0)) { System.out.println("Congratulations on winning the fourth prize! 200 yuan!!!"); } else if (countRed == 5 && countBlue == 1) { System.out.println("Congratulations on winning the third prize! 3000 yuan!!!!"); } else if (countRed == 6 && countBlue == 0) { System.out.println("Congratulations on winning the second prize! "+ 500*0.3 +"Ten thousand yuan w(゚Д゚)w!!!!!"); } else { System.out.println("Congratulations on winning the first prize! 5 million yuan (^o^) / w(゚Д゚)w!!!!!"); } } // Random generation of two-color spheres public static int[] randomArray() { int[] array = new int[7]; // Generate 6 different random numbers to represent 6 red balls for (int i = 0; i < 6; i++) { boolean flag = true; // Define a flag to judge whether the red ball number is repeated int randomNum = (int)(Math.random() * 33 + 1); // Randomly generate a red ball number (1 ~ 33) // Only the red ball requires different numbers to traverse the array, so you only need to traverse the first 6 numbers for (int j = 0; j < 6; j++) { if (randomNum == array[j]) { // Compare all the numbers in the array with the newly generated random numbers i--; // Roll i back flag = false; // Indicates repetition break; } } if (flag) { // If not repeated array[i] = randomNum; // Store the randomly generated red ball number in the array } } array[6] = (int)(Math.random() * 16 + 1); // Randomly generate basketball numbers 1 ~ 16 return array; } // Sort (from small to large) bubble public static int[] sort (int[] a) { // Only the first 6 numbers are sorted, so it is a.length-2 here for (int i = 0; i < a.length-2; i++) { for (int j = 0; j < a.length-i-2; j++) { if (a[j] > a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } return a; }
summary
This is a small case in the study. The author is still studying hard. If he is not thoughtful, please criticize and correct all the gods and learn together.