java implementation function - Dichroic sphere

Keywords: Java

;

To realize the two color sphere, the overall idea should be considered first:
1. Randomly generate 7-digit array as the number of the grand prize (the value range of each value of the first 6 red balls is 1-33, and the 6 values cannot be the same, and the value range of the seventh basketball is 1-16)
2. Judge whether the purchase number is entered manually or selected by machine (if it is entered manually, input a value from the console to the array according to the number of times with for loop once; if it is selected by machine, the implementation code is the same as that in 1.)
3. The first six red balls of the prize number and purchase number are arranged in ascending order
4. Judge the number matching degree between the prize number and the purchase number to determine the prize winning level or not, and output the result to the console

The implementation code is as follows:

//Guide bag
import java.util.*;
class TestDouble01 
{
    public static void main(String[] args) 
    {
        Scanner s = new Scanner(System.in);
        Random ran = new Random();
        //Randomly generate prize number
        int arr2[]=new int[7];
        int shu =33;
        for (int i=0;i<arr2.length ;i++ )
        {
            boolean flag2=true;
            while (flag2)
            {
                if(i==6){
                    shu = 16;
                }
                int b = ran.nextInt(shu)+1;
                int num2=0;
                for (int j=0;j<arr2.length ;j++ )
                {
                    if (b==arr2[j])
                    {
                        num2++;
                    }
                }
                if (num2==0)
                {
                    arr2[i]=b;
                    flag2=false;
                }
            }
        }
        //Determine the generation method of purchase number
        boolean flag=true;

        int arr1[]=new int[7];
        while(flag){
            System.out.println("Please select machine selection or manual selection: 1. Machine selection; 2. Manual selection; 3. Exit (please enter the corresponding serial number)");
            int xz=s.nextInt();
            if (xz == 1)
            {
                int shu1 = 33;
                //Randomly generate purchase number
                for (int i=0;i<arr1.length ;i++ )
                {
                    boolean flag1=true;
                    while (flag1)
                    {
                        if (i==6)
                        {
                            shu1=16;
                        }
                        int a = ran.nextInt(shu1)+1;
                        int num1=0;
                        for (int j=0;j<arr1.length ;j++ )
                        {
                            if (a==arr1[j])
                            {
                                num1++;
                            }
                        }
                        if (num1==0)
                        {
                            arr1[i]=a;
                            flag1=false;
                        }
                    }
                }
                flag=false;
            }else if (xz==2)
            {   
                //Manual input
                for (int i=0;i<arr1.length ;i++ )
                {
                    System.out.println("Please enter the"+(i+1)+"number:");
                    arr1[i]=s.nextInt();
                }
                s.close();
            }else if (xz==3)
            {
                //sign out
                flag=false;
            }else{
                System.out.println("Please enter the correct serial number");
            }
        }
        //Rank the first six red balls in ascending order
        for(int j=0;j<=arr1.length-2;j++){
            for (int i = 0;i<arr1.length-2-j;i++ )
            {
                if (arr1[i]>arr1[i+1])
                {
                    arr1[i]=arr1[i]+arr1[i+1];
                    arr1[i+1]=arr1[i]-arr1[i+1];
                    arr1[i]=arr1[i]-arr1[i+1];
                }
            }
        }
        //Rank the first six red balls of random purchase array in ascending order
        for(int j=0;j<=arr2.length-2;j++){
            for (int i = 0;i<arr2.length-2-j;i++ )
            {
                if (arr2[i]>arr2[i+1])
                {
                    arr2[i]=arr2[i]+arr2[i+1];
                    arr2[i+1]=arr2[i]-arr2[i+1];
                    arr2[i]=arr2[i]-arr2[i+1];
                }
            }
        }

        //Print prize number and purchase number
        System.out.println("Purchase number:"+Arrays.toString(arr1));
        System.out.println("The prize number is:"+Arrays.toString(arr2));


        //Judging Awards
        //Query the same number of the first six digits in two arrays
        int num = 0;
        for (int i=0;i<arr1.length-1 ;i++ )
        {
            for (int j=0;j<arr2.length-1 ;j++ )
            {
                if (arr2[i]==arr1[j])
                {
                    num++;
                }
            }
        }
        //Judging awards by the same number
        System.out.println(num);
        System.out.println(arr1[6]==arr2[6]);
        if (arr1[6]==arr2[6])
        {
            if (num==6){
                System.out.println("Congratulations on winning the first prize");
            }else if (num==5){
                System.out.println("Congratulations on your third prize");
            }else if (num==4){
                System.out.println("Congratulations on winning the fourth prize");
            }else if (num==3){
                System.out.println("Congratulations on winning the fifth prize");
            }else if (num==2 || num==1 || num==0){
                System.out.println("Congratulations on winning the sixth prize");
            }else{
                System.out.println("You haven't won the prize, keep up your efforts");
            }
        }else{
            if (num==6){
                System.out.println("Congratulations on your two prizes");
            }else if (num==5){
                System.out.println("Congratulations on winning the fourth prize");
            }else if (num==4){
                System.out.println("Congratulations on winning the fifth prize");
            }else{
                System.out.println("You haven't won the prize, keep up your efforts");
            }
        }
    }
}

The code runs as follows:

Because winning is a probability problem, the probability of winning a prize is very low. Most of them are "not winning" which is a normal phenomenon.

Posted by nolos on Fri, 15 May 2020 10:10:57 -0700