A self-written Java program for playing cards

Source: Internet
Author: User

Yesterday, I went to a friend's company and Sang-your mobile phone KTV (for a little publicity: currently, the App Store ranks very top in the rankings) for an interview. The result is XX.

One of the cards washing procedures was written on the draft paper yesterday. The interviewer said that the description was not clear. Today, I took the time to implement it on the computer to end the interview.

Package COM. bankht. test. xipai; import Java. util. random; import Org. JUnit. test;/*** @ Author: -AK47 * @ Creation Time: 09:44:39 ** @ Class description: shuffling algorithm */public class xipaitest {private string [] singlecards = new string [] {"2", "3", "4", "5", "6 ", "7", "8", "9", "10", "J", "Q", "k", ""}; private string [] allcards = new string [54]; private string temp = NULL; // defines a temporary variable for exchanging the positions of two values in the array @ testpublic void testxipai () {string [] cards = initcard (); For (int s = 0; S <1000; s ++) {/** two numbers cardnum1 are randomly selected from, cardnum2, * swap the corresponding array value location (cards [cardnum1] <--> cards [cardnum2]) * this step loops 1000 times */INT cardnum1 = new random (). nextint (54); int cardnum2 = new random (). nextint (54); temp = cards [cardnum1]; cards [cardnum1] = cards [cardnum2]; cards [cardnum2] = temp;} testcardmessage (CARDS ); // test the current sorting status of playing cards}/*** initialize playing cards ** @ return playing card array */private string [] initcard () {for (INT I = 0; I <13; I ++) {string [] initcard = addmessage2card (singlecards [I]); For (Int J = 0; j <4; j ++) {allcards [I * 4 + J] = initcard [J] ;}} allcards [52] = "Xiao Wang"; allcards [53] = ""; return allcards ;} /*** for 2, 3, 4... q, K, and a all have public information [red peach, square, plum blossom, and black peach ], add ** @ Param card * @ return */private string [] addmessage2card (string card) {return New String [] {"Peach" + card, "Side slice" + card, "Plum Blossom" + card, "Black peach" + card };} /*** test the playing card's current sorting status ** @ Param allcards */private void testcardmessage (string [] allcards) {for (INT m = 0; m <allcards. length; m ++) {system. out. print (allcards [m] + ","); If (m + 1) % 4 = 0) {system. out. println ();}}}}

Run:

Side slice K, black peach 8, plum blossom 2, red peach 2, plum blossom 5, plum blossom 8, red peach K, black peach 9, red peach 4, side slice 9, plum blossom J, side slice 8, plum blossom Q, plum Blossom 4, black peach Q, red peach 10, black peach 7, side slice 5, red peach 3, King, red peach 6, black peach J, side slice 2, side slice a, black peach 2, black peach A, plum blossom, side slice 3, red peach 7, black peach 10, side slice 6, plum blossom 10, side slice Q, small king, plum blossom 3, red peach 8, black peach 6, black peach K, black peach 3, red peach J, red peach, peach 5, peach 5, plum blossom 6, cube J, cube 4, peach Q, peach 4, peach 9, plum blossom 7, cube 7, plum blossom K, cube 10, plum blossom 9,

 

In fact, it's just the idea that two random numbers correspond to the positions of playing cards to exchange each other. As for restoring a pair of playing cards, we will use the commonly used sorting (such as sorting, Hill, and bubble ).

Finally: I have lost hope that the software will continue to develop rapidly. In any case, I would like to thank Xiao Kai for his suggestion and Director Tian ran for his suggestion.

Related Article

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.