java self-study -- array tool class and two-color ball case

definition Array utility class: various methods used to manipulate arrays, such as sorting and searching To use this met...
Playing method of two-color lottery
analysis
Implementation steps
Code example

definition

Array utility class: various methods used to manipulate arrays, such as sorting and searching
To use this method, you need to import the toolkit: import java.util.Arrays;

common method
  • Binary search: arrays.binarysearch (int [] array, int value);
  • The contents of the array are converted into a string for output: arrays.tostring (int [] array);
  • Array sorting: arrays.sort (int [] array)
  • Copy the specified array:
  • Arrays.copyOf(int [] arrary, int length); Copy array by length
  • System.arraycopy(Object src, int srcPos, Object dest, int destPos,int length);, Parameter information: original object, the starting position of the original object; Target array, the starting position of the target array; Copy length
  • Judge whether two arrays are equal: arrays.equals (int [] array_1, int array_2);
  • Fill the array with the specified elements: arrays. Fill (int [] array, int value);
  • The order of array replication efficiency from high to low is: arraycopy - > copyof - > for cyclic reading
Code example
package class_1006; import java.util.Arrays; public class class_arrary { public static void main(String[] args) { // TODO Auto-generated method stub int[] num = ; int index_t = Arrays.binarySearch(num, 99);//Binary search_ Can find System.out.println("The subscript in the sequence is:"+index_t); int index_f = Arrays.binarySearch(num, 0);//Binary search_ can't find System.out.println("The subscript in the sequence is:"+index_f);//The return value is - (low+1) int [] data = ; System.out.println("data After array conversion:"+Arrays.toString(data));//The contents of the array are converted into string s and output directly String new_num = Arrays.toString(num); System.out.println("num After array conversion:"+new_num);//Assign value to variable before output Arrays.sort(data);//Quick sort is used System.out.println("num After array sorting conversion:"+Arrays.toString(data));//Output string after sorting //Copy the specified array according to the length. If the set length is greater than the length of the array, the default value of the extra array is 0 int[] new_data = Arrays.copyOf(data, 10); System.out.println("data After the array is copied and replaced:"+Arrays.toString(new_data)); //In this way, the length parameter controls the number of copied array elements; The starting position of the original array controls where to start the value //The starting position of the new array controls where to start writing int[] new_data_1 = new int[data.length];//Create a new array and define the length System.arraycopy(new_data, 2, new_data_1, 2,3);//Copies the specified array System.out.println("data After copying the array:"+Arrays.toString(new_data_1)); boolean result = Arrays.equals(data, num);//Determine whether two arrays are equal System.out.println("The comparison result of the two arrays is:"+result); Arrays.fill(num, 1000);//Fills the data filled element with the specified element System.out.println("The result after filling is:"+Arrays.toString(num)); } } //result The subscript in the sequence is: 8 The subscript in the sequence is:-1 data After array conversion:[101, 10, 88, 55, 66, 77, 33, 200, 109] num After array conversion:[11, 22, 33, 44, 55, 66, 77, 88, 99, 101] num After array sorting conversion:[10, 33, 55, 66, 77, 88, 101, 109, 200] data After the array is copied and replaced:[10, 33, 55, 66, 77, 88, 101, 109, 200, 0] data After copying the array:[0, 0, 55, 66, 77, 0, 0, 0, 0] The comparison result of the two arrays is: false The result after filling is:[1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000]
Two color ball case

Playing method of two-color lottery

Two color ball betting is divided into red ball number area and basketball number area. The red ball number area is 01-33 and the basketball number area is 01-16; The two-color ball has six numbers from 33 red balls in each issue, and one number from 16 blue balls is a good way to win the prize. Even if you guess the six red ball numbers and one blue ball number of the lottery number, the order is unlimited

  • First prize: 6 red balls and 1 blue ball are all correct
  • Second prize 6 red balls correct
  • Third prize 5 red balls and 1 blue ball correct
  • The fourth prize is 5 red balls correct or 4 red balls and 1 blue ball correct
  • The fifth prize is 4 red balls correct or 3 red balls and 1 blue ball correct
  • The sixth prize is 1 red ball, 1 blue ball, 2 red balls, 1 blue ball and 1 blue ball

analysis

  • How to produce blue ball and red ball—— generation of random number
  • How to receive user selected numbers?
  • How to verify whether winning?
  • Announce the winning numbers of this issue

Implementation steps

  • Overall realization idea
    Step 1: the user selects whether it is machine selection or first choice; Machine selected random production, user input is preferred
    Step 2: receive user selected number (6 red and 1 blue)
    Step 3: system generated number (6 red and 1 blue)
    Step 4: compare the number with the user number and calculate the equivalent number
    Step 5: verify whether you win the prize
    Step 6: system number sorting
    Step 7: give the results

  • Random value non repetition algorithm (system and user)

  • The logic of calculating whether to win the prize

  • Result output

Code example

package class_1006; import java.util.Random; import java.util.Scanner; import java.util.Arrays; public class class_two_color { public static void main(String[] args) { // TODO Auto-generated method stub //Preparatory work - define relevant variables int[] userRedBall = new int[6];//Define the red ball number selected by the user int userBlueBall = 0;//The blue ball number selected by the user, because there is only 1, is given a default value of 0 int[] rebortRedBall = new int[6];//Defines the red ball number selected by the system int rebortBlueBall = 0;//System selected basketball number int redCount = 0; //The user selects the correct number of red balls int blueCount = 0; //The user selects the correct number of red balls //It is necessary to randomly generate 6 numbers that are not repeated between 1-33 (algorithm) //Implementation method: after the random number is generated, change the position between the random number and the last bit in the array, and the next time it is random, the array - 1 int[] redBall = new int[33];//An array used to randomly generate red ball numbers for(int i=0;i<redBall.length;i++){ redBall[i] = i+1; } //At the beginning of the game, the user needs to select the number, machine selection or manual input System.out.println("The two-color ball game begins. Good luck!"); System.out.println("Please select the way to purchase the number (1: machine selection, 2: manual selection):"); Scanner input = new Scanner(System.in); boolean flag = true; // while(flag){ int isAuto = input.nextInt();//Use a variable to receive the results of the console //Select the number by machine or hand according to the console results switch (isAuto) { case 1: //Machine selection //redBall is an array of 1-33. userRedBall is the red ball number generated by the user. It is not repeated and corresponds to the following computerSelect(redBall,userRedBall);//Machine selected red ball Random r = new Random(); userBlueBall = r.nextInt(16)+1;//The blue ball generated randomly in 16 bits should be + 1, because it starts from 0 flag = false;//The loop exits, so the value of flag is changed break; case 2: //Hand selection System.out.println("Please select 1-33 The 6 red ball numbers between cannot be repeated"); for(int i=0;i<userRedBall.length;i++){ userRedBall[i] = input.nextInt(); } System.out.println("Please select 1-16 1 basketball number between"); userBlueBall = input.nextInt(); flag = false; break; //If the value entered is not 1 or 2, go to this step default: System.out.println("Please select the way to purchase the number (1: machine selection, 2 "preferred):"); } } //The system randomly generates red balls and puts them into the array of red balls computerSelect(redBall,rebortRedBall); //System randomly generates blue ball Random r = new Random(); rebortBlueBall = r.nextInt(16)+1; //Statistical results, statistical red balls, judge several guesses for(int i=0;i<userRedBall.length-redCount;i++){ for(int j=0;j<rebortRedBall.length;j++){ //If two numbers are equal if(userRedBall[i]==rebortRedBall[j]){ //Swap equal values to the last bit int temp = rebortRedBall[j]; rebortRedBall[j]=rebortRedBall[rebortRedBall.length-1-redCount]; rebortRedBall[rebortRedBall.length-1-redCount]=temp; redCount++; break; } } } //Statistical basketball if(userBlueBall==rebortBlueBall){ blueCount = 1; } //Verify whether the winner if(blueCount==0 && redCount<=3){ System.out.println("Sorry, you didn't win the prize"); }else if(blueCount==1 && redCount<3){ System.out.println("Congratulations, you won the sixth prize, which can be exchanged for 5 yuan"); }else if((blueCount==1 && redCount==3)||(blueCount==0 && redCount==4)){ System.out.println("Congratulations, you won the fifth prize, which can be exchanged for 10 yuan"); }else if((blueCount==1 && redCount==4)||( blueCount==0&&redCount==5)){ System.out.println("Congratulations, you won the fourth prize, which can be exchanged for 200 yuan"); }else if(blueCount==1 && redCount==5){ System.out.println("Congratulations, you won the third prize, which can be exchanged for 3000 yuan"); }else if(blueCount==0 && redCount==6){ System.out.println("Congratulations, you won the second prize, which can be exchanged for 150000 yuan"); }else if(blueCount==1 && redCount==6){ System.out.println("Congratulations, you won the first prize, which can be exchanged for 5 million yuan"); }else { System.out.println("System error, winning is invalid!"); } //Output - publish system number System.out.println("The red ball number of this issue is:"); sort(rebortRedBall); System.out.println(Arrays.toString(rebortRedBall)); System.out.println("The winning basketball number of this issue is:"+rebortBlueBall); //Publish user number System.out.println("The red ball number you selected is:"); sort(userRedBall); System.out.println(Arrays.toString(userRedBall)); System.out.println("The winning basketball number of this issue is:"+userBlueBall); } //Bubble sorting 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++){ if(ball[j]>ball[j+1]){ ball[j]=ball[j]+ball[j+1]; ball[j+1]=ball[j]-ball[j+1]; ball[j]=ball[j]-ball[j+1]; } } } } //Randomly generate multiple non repeating red balls in the specified sequence //ballCoubnt is a sequence of 1-33; redBall is an array placed for the generated 6 numbers public static void computerSelect(int [] redBall,int[] userRedball){ Random r = new Random(); int index = -1;//The subscript is put here because it needs to be reused for(int i =0;i<=userRedball.length-1;i++){ //Randomly generate several numbers and subtract a few from the length to ensure that the selected values are not repeated index = r.nextInt(redBall.length-i);//The subscript of a number is randomly generated in the sequence of 1-33 //The index subscript is the number stored in the selected value and is placed at the end of the sequence userRedball[i]=redBall[index];//Place the value of the subscript in the new sequence int temp = redBall[index];//Intermediate variable, temporarily put the stored value for exchange //0 should be exchanged with the last value of the sequence. When i=1, it should be exchanged with the last value of the sequence redBall[index]= redBall[redBall.length-1-i]; redBall[redBall.length-1-i]=temp; } } } //result The two-color ball game begins. Good luck! Please select the way to purchase the number (1: machine selection, 2: manual selection): 1 Sorry, you didn't win the prize The red ball number of this issue is: [3, 10, 18, 21, 25, 32] The winning basketball number of this issue is: 11 The red ball number you selected is: [3, 18, 23, 24, 26, 30] The winning basketball number of this issue is: 16

6 October 2021, 12:26 | Views: 2235

Add new comment

For adding a comment, please log in
or create account

0 comments