About the case

illustrate

The two-color ball betting area is divided into the red ball number area and the blue ball number area. The red ball number ranges from 01 to 33, and the blue ball number ranges from 01 to 16. In each period, 6 numbers are drawn from 33 red balls, and 1 number is drawn from 16 blue balls as the winning number. The double-color ball game is to guess the 6 red ball numbers and 1 blue ball number of the lottery number. The order is not limited. Buyers can choose machine-selected number betting and self-selected number betting. Machine-selected number betting refers to the betting number randomly generated by the betting machine for betting, and self-selected number betting refers to inputting the number selected by the buyer into the betting machine for betting. The gameplay is as shown in the figure:

case analysis

  1. How to generate blue and red balls?

  1. How to receive user selection number?

  1. How to verify whether you have won the lottery?

  1. How to announce the winning numbers for this period?

Implementation steps

  1. The overall realization of ideas.

  1. Random value non-repetition algorithm (system and user)

  1. The logic of judging whether to win the lottery.

  1. The result output.

problem thinking

  1. The user chooses whether to select by machine or by hand

  1. Receive user selection number (6 red, 1 blue)

  1. Generate system number (6 red, 1 blue)

  1. Compare the system number and user number, record the number

  1. Verify if you have won

  1. System number sorting

  1. Announce results

code show as below

  1. Define relevant variables
public static void main(String[] args) {
        int[] userRedBall =new int[6];  //User selected red ball
        int[] sysRedBall = new int[6];  //The red ball selected by the system
        int userBlueBall = 0;           //Basketball by user selection
        int sysBlueBall = 0;            //basketball chosen by the system
        int redCount = 0;               //Record the user's selection of the correct red ball
        int blueCount = 0;              //Record the user's selection of the correct basketball
        int[] redBall = new int[33];        //The number used to store the red ball
        //Need to randomly generate 6 non-repeating numbers between 1-33
        for(int i=0;i<redBall.length;i++){
            redBall[i]=i+1;
        }
2. The user chooses manual selection and machine selection.

The switch loop can be used here, while here the user wants to input 1, and the number other than 2 can be reselected.

 System.out.println("Choose the way you want to choose (1.machine selection 2.hand selected)");
 boolean flag=true;
  while(flag){
            int isAoTo = input.nextInt();
            switch(isAoTo){
                case 1: {         //machine selection
                }
                break;                      
                case 2: {        //Manual selection
                }                           
                default:
                    System.out.println("The way you choose is wrong, please choose again");
                    break;
            }

        }
3. Define a computerSelect method

Define a computerSelect method here to randomly select numbers, and define that this method can be called multiple times. The method of computer selection is similar to the method of system number generation.

public static void computerSelect(int[] redBall,int[] userRedBall){
        Random r =new Random();
        int index=-1;
        for (int i=0;i<userRedBall.length;i++){
            index=r.nextInt(redBall.length-1-i);
            userRedBall[i]=redBall[index];

            int temp = redBall[index];
            redBall[redBall.length-1-i]=temp;
            redBall[index] = redBall[redBall.length-1-i];//Let the large numbers go to the back, reduce the length of the array, and improve code efficiency
        }
4. Determine the user selection number

Complete the switch loop above and call the computerSelect method

while(flag){
            int isAoTo = input.nextInt();
            switch(isAoTo){
                case 1: {
                    computerSelect(redBall,userRedBall);//randomly selected red ball
                    userBlueBall = r.nextInt(16);
                     flag = false;
                }
                break;                      //machine selection
                case 2: {
                    //red ball selection
                    System.out.println("Please choose six red ball numbers (1-33)");
                    for(int i=0;i<userRedBall.length-1;i++){
                        userRedBall[i] = input.nextInt();
                    }
                    //basketball selection
                    System.out.println("Please select a basketball number (1-16)");
                    userBlueBall = input.nextInt();
                    flag = false;
                    break;
                }                           //Manual selection
                default:
                    System.out.println("The way you choose is wrong, please choose again");
                    break;
            }
5. System generated number

The number generated by the system is the same as the method of computer selection, here directly call the computerSelect method

     //The system randomly generates red balls
     computerSelect(redBall,sysRedBall);

     //system generated basketball
      sysBlueBall =r.nextInt(16);

6. Statistical results
//statistical results
        //red ball result
        for (int i=1;i<userRedBall.length;i++){
            for (int j=0;j<sysRedBall.length;j++){
                if(userRedBall[i]==sysRedBall[j]){
                    redCount++;
                    //The algorithm here can be optimized by exchanging the compared number with the last number; it is the same as the thinking algorithm of large numbers in 3.
                }
            }
        }
        //basketball result
        if(userBlueBall == sysBlueBall){
            blueCount++;
        }
7. Judging whether the
//Verify if you have won
        if(blueCount==0 && redCount<=3){
            System.out.println("Didn't win");
        }else if(blueCount==1 && redCount<3){
            System.out.println("Sixth Prize");
        }else if((blueCount==1 && redCount==3)||blueCount==0 && redCount==4 ){
            System.out.println("fifth prize");
        }else if((blueCount==1 && redCount==4)||blueCount==0 && redCount==5 ){
            System.out.println("Fourth Prize");
        }else if((blueCount==1 && redCount==5)||blueCount==0 && redCount==6){
            System.out.println("third prize");
        }else if(blueCount==0 && redCount==6 ){
            System.out.println("second prize");
        }else if(blueCount==1 && redCount==6){
            System.out.println("first prize");
        }

    }
8. Verify whether you have won the prize

The sorting method can be used to sort the selected numbers, and it is more intuitive to compare with the numbers selected by the system.

//Publish System Number
System.out.println("The winning red number for this draw is");
sort(sysRedBall);
System.out.println(Arrays.toString(sysRedBall));
System.out.println("The winning basketball number in this issue is"+sysBlueBall);
//Publish user number
System.out.println("Your red number is");
sort(userRedBall);
System.out.println(Arrays.toString(userRedBall));
System.out.println("Your basketball number is"+userBlueBall);

//Bubble Sort
public static void sort(int[] ball){
for(int i=0;i<ball.length-1;i++){
for(int j=0;j<ball.length-1-i;j++){
    int temp = ball[j];
    ball[j] =ball[j+1];
    ball[j+1]=temp;
}

full code

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class ShuangsheqiuAnli {
    public static void main(String[] args) {
        int[] userRedBall =new int[6];  //User selected red ball
        int[] sysRedBall = new int[6];  //The red ball selected by the system
        int userBlueBall = 0;           //Basketball by user selection
        int sysBlueBall = 0;            //basketball chosen by the system
        int redCount = 0;               //Record the user's selection of the correct red ball
        int blueCount = 0;              //Record the user's selection of the correct basketball
        int[] redBall = new int[33];        //The number used to store the red ball
                                //Need to randomly generate 6 non-repeating numbers between 1-33
        for(int i=0;i<redBall.length;i++){
            redBall[i]=i+1;
        }
        System.out.println("Choose the way you want to choose (1.machine selection 2.hand selected)");
        Scanner input =new Scanner(System.in);  //Declare a reference to the Scanner class
        Random r =new Random();

        boolean flag=true;

        while(flag){
            int isAoTo = input.nextInt();
            switch(isAoTo){
                case 1: {
                    computerSelect(redBall,userRedBall);//randomly selected red ball
                    userBlueBall = r.nextInt(16);
                     flag = false;
                }
                break;                      //machine selection
                case 2: {
                    //red ball selection
                    System.out.println("Please choose six red ball numbers (1-33)");
                    for(int i=0;i<userRedBall.length-1;i++){
                        userRedBall[i] = input.nextInt();
                    }
                    //basketball selection
                    System.out.println("Please select a basketball number (1-16)");
                    userBlueBall = input.nextInt();
                    flag = false;
                    break;
                }                           //Manual selection
                default:
                    System.out.println("The way you choose is wrong, please choose again");
                    break;
            }

        }
        //The system randomly generates red balls
        computerSelect(redBall,sysRedBall);

        //system generated basketball
        sysBlueBall =r.nextInt(16);
        //statistical results
        //red ball result
        for (int i=1;i<userRedBall.length;i++){
            for (int j=0;j<sysRedBall.length;j++){
                if(userRedBall[i]==sysRedBall[j]){
                    redCount++;
                    //Here the algorithm can be optimized to exchange the compared number with the last number;
                }
            }
        }
        //basketball result
        if(userBlueBall == sysBlueBall){
            blueCount++;
        }
        //The results show


        //Verify if you have won
        if(blueCount==0 && redCount<=3){
            System.out.println("Didn't win");
        }else if(blueCount==1 && redCount<3){
            System.out.println("Sixth Prize");
        }else if((blueCount==1 && redCount==3)||blueCount==0 && redCount==4 ){
            System.out.println("fifth prize");
        }else if((blueCount==1 && redCount==4)||blueCount==0 && redCount==5 ){
            System.out.println("Fourth Prize");
        }else if((blueCount==1 && redCount==5)||blueCount==0 && redCount==6){
            System.out.println("third prize");
        }else if(blueCount==0 && redCount==6 ){
            System.out.println("second prize");
        }else if(blueCount==1 && redCount==6){
            System.out.println("first prize");
        }
        //Publish System Number
        System.out.println("The winning red numbers for this draw are");
        sort(sysRedBall);
        System.out.println(Arrays.toString(sysRedBall));
        System.out.println("The winning basketball number in this issue is"+sysBlueBall);
        //Publish user number
        System.out.println("Your red number is");
        sort(userRedBall);
        System.out.println(Arrays.toString(userRedBall));
        System.out.println("Your basketball number is"+userBlueBall);
    }


                    //Define a method for the algorithm used to select random numbers in the specified sequence;
                  //The redBall array represents the number of red balls stored
                //userRedBall randomly selected user number (requires storage)
                    //Bubble Sort

    public static void sort(int[] ball) {
        for (int i = 0; i < ball.length - 1; i++) {
            for (int j = 0; j < ball.length - 1 - i; j++) {
                int temp = ball[j];
                ball[j] = ball[j + 1];
                ball[j + 1] = temp;
            }
        }
    }

     public static void computerSelect(int[] redBall,int[] userRedBall){
        Random r =new Random();
        int index=-1;
        for (int i=0;i<userRedBall.length;i++){
            index=r.nextInt(redBall.length-1-i);
            userRedBall[i]=redBall[index];

            int temp = redBall[index];
            redBall[redBall.length-1-i]=temp;
            redBall[index] = redBall[redBall.length-1-i];
        }

    }
}

Tags: Java

Posted by djelica on Wed, 22 Feb 2023 15:42:17 +1030