The title is: lottery is to randomly select several non repeated values from multiple numbers, sort and output them, and judge whether they win the prize. If you win, the number of winning times will be output; if you don't win, a prompt will be given.
Example of output results:
Codes can be:
package Three.NewLearn; import java.util.Arrays; import java.util.Scanner; public class Lottery game { public static void main(String[] args) { int count = 0; //Define a measurement number and calculate how many times you won the prize Scanner sc = new Scanner(System.in); System.out.println("Please enter the maximum number of numbers you want to extract:"); int n = sc.nextInt(); //Set a n to store the highest value int n1 = n; //Defines a value where n1 and n are the same System.out.println("Please enter the number you want to extract(1~"+n+"):"); int k = sc.nextInt(); //The number of decimals is set if(k>n){ System.out.println("Sorry, the number you entered does not meet the specification!"); //If the extracted number is lower than the maximum number at this time, an error is reported }else{ int[] numbers = new int[n]; int[] numbers2 = new int[n1]; //Two arrays with the same length as n and n1 are defined for (int i = 0; i < numbers.length; i++) { numbers[i] = i+1; //Assign values from 1 to n to the array numbers; } for (int i = 0; i < numbers2.length; i++) { numbers2[i] = i+1; //Assign values from 1 to N1 to array numbers2; } int[] result = new int[k]; //The result array is defined so that its length is equal to the number of extracts k for (int i = 0; i < result.length; i++) { int r =(int) (Math.random()*n); //At this time, Math.random()*n is of double type and needs to be cast to int type. result[i] = numbers[r]; //Select the random number numbers[r] and assign it to result[i] numbers[r] = numbers[n - 1]; n--; //This ensures that the same value will not be extracted again } int[] winning = new int[k]; for (int i = 0; i < winning.length; i++) { int w = (int) (Math.random()*n); winning[i] = numbers2[w]; numbers2[w] = numbers2[n1 - 1]; n1--; } Arrays.sort(result); //Sort arrays System.out.println("The extraction is complete. The number you extracted is:"); for(int r :result){ System.out.print(r+" "); } //Use the foreach loop to traverse the array System.out.println(""); //Line feed Arrays.sort(winning); System.out.println("The number of winners is:"); for (int w:winning){ System.out.print(w+" "); } for (int x = 0; x < result.length; x++) { for (int y = 0; y < winning.length; y++) { if(result[x] == winning[y]){ count++; } } } //Use two for loops and use the if statement to judge the value of the array //If there is the same value, the count value will increase automatically System.out.println(""); if(count == 0){ System.out.println("Sorry, you didn't win the prize. Please keep up your efforts!"); }else{ System.out.println("Congratulations, you won the prize"+count+"second"); } } } }
Introduce the following ideas:
Because this game requires keyboard input in the whole process, I first defined a Scanner method.
The lottery game needs to define its range first, so I define an integer n to store its highest value, and then define an integer k to store the number I need to draw. Then why is there a n1=n? We'll talk about this later.
After input, it is necessary to judge the values of N and K. if n > k, it is OK to continue the following procedure. If n < K, it is not OK, because the value to be extracted here is not repeated, and the extracted quantity is larger than the defined range. Therefore, we use the if else statement to judge.
Next, I define an array numbers to store the lottery values, so I store all the values 1, 2, 3, ···, n in the array numbers. At this time, I can use the for loop to assign values to the array.
The third array result and the fourth array winning store the extracted numbers, in which the Math.random method is used to return a random floating-point number between 0 and 1 (including 0 and excluding 1). At this time, if n is used to multiply the floating-point number, a random number from 0 to n-1 can be obtained, but the floating-point number is still obtained at this time, so I convert the floating-point number to int type by coercion and use a variable r to store the value of the random number.
Next is the key point. How to solve the problem of repetition?
Here, i set the ith element of result to the value stored in numbers[r], initially r+1
Namely:
result[i] = numbers[r];
If set in this way, the contents of the numbers array will change after each extraction.
So here I'll overwrite numbers[r] with the last number in the array and subtract n by 1
Namely:
numbers[r] = numbers[n-1]; n--;
The key is that each extraction is a subscript (index), not the actual value. The subscript points to the value in the array that has not been extracted.
The winning array can no longer use the value of n, otherwise the extracted values of the two arrays will never be the same, so it is necessary to redefine an array and a variable to count and extract again. The extraction principle is the same as above.
After k numbers are extracted, the result array can be sorted, and the Arrays.sort() method can be used.
Finally, two for statement loops are used to judge whether the values in the two extracted arrays are the same. If they are the same, the counted variable count will increase automatically.
Finally, output the results.