Java Programming practices

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.