Creates an integer array of 54 elements and assigns its element values to: 1~54, which is used to represent 54 cards of a deck. Create an integer array of 12 elements to represent the hand of a player, and then randomly extract 12 elements from the previous array to the array. Prints the value of the following array. (You can solve this problem without considering the problem of duplicate extraction, if you have the ability.)
Program code:
Public classTest1 {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//create an integer array of 54 elements a[54] int[] A =New int[54]; System.out.println (The array of "54 elements a[54] is as follows: \ n (here 6 in each row, a total of 9 rows of squares shown)"); for(inti=0;i<54;i++) {A[i]=i+1; System.out.print (A[i]+" "); if(a[i]%9==0) {System.out.println (""); } } //creates an integer array of 12 elements that are randomly extracted from an array of a[54] (extracted by array subscript) B[12] int[] B =New int[12]; intp,q; for(p = 0; p < b.length; p++) {Q=(int) (Math.random () * 53);//Q take 0~53 any value, that is, the array subscript, where the random () value: 0~1b[p]=A[q]; //If duplicate elements are found, remove the element and re-fetch the value until it is not repeated for(intj = 0; J < P; J + +) { if(b[j]==B[p]) {P--; Break; } } } //prints the array of 12 elements b[12] randomly extracted from the array a[54]. System.out.println ("\ n the array of 12 non-repeating elements that are randomly extracted b[12] is:"); for(p = 0; p <12; p++) {System.out.print (B[p]+" "); } }}
The results are as follows:
Java Programming practices