Guess digital game (Java swing interface implementation)

Source: Internet
Author: User

In the Java-written wenquxing guess digital game, the system will automatically generate a four-digit number with different digits. When you guess this number, there are two types of prompts: A and B, A indicates that the number is in the correct position, and B indicates that the number is only in the correct position. When you guess all the numbers, it is successful (that is, 4a0b ).

Enter a number with different four digits. The system automatically checks whether the number matches. If the number does not match, a nanb prompt is displayed, asking the player to enter another number.

Package COM. xujin; import Java. util. arraylist; import Java. util. random; import Java. util. extends; public class guess {extends CIN = new evaluate (system. in); public static void main (string... ARGs) {guess = new guess (); system. out. println ("welcome to the guessnum game, enjoy it! "); // Produce a new number which to guess. // use an arraylist to randomly select a 0 ~ After the number of 9 is selected, delete it in the arraylist, and then randomly select a number in it, until all four are selected // initialize the arraylistarraylist <integer> array = new arraylist <integer> (); For (INT I = 0; I <10; I ++) array. add (I); random = new random (); For (INT I = 0; I <4; I ++) {int singlenumindex = random. nextint (arrayrange --); aimnum. add (array. get (singlenumindex); array. remove (singlenumindex);} // test if the guess number equals to the aim number. while (true) {Guess. Input (); If (aimnum. Equals (guessnum) {system. Out. println ("Congratulations! You make it! "); Break;} Guess. output (); guessnum. clear () ;}} private void input () {int inputnum = cin. nextint (); For (INT I = 0; I <4; I ++) {guessnum. add (0, inputnum % 10); inputnum = inputnum/10;} private void output () {for (INT I = 0; I <4; I ++) if (aimnum. get (I) = guessnum. get (I) acount ++; For (int I: aimnum) for (Int J: guessnum) if (I = J) bcount ++; bcount-= acount; system. out. println (acount + "A" + bcount + "B"); acount = 0; bcount = 0;} Private Static int seqnum = 0; Private Static int arrayrange = 10; // upper bound, used to randomly generate an array subscript Private Static arraylist <integer> aimnum = new arraylist <integer> (); private Static arraylist <integer> guessnum = new arraylist <integer> (); Private Static int acount = 0; Private Static int bcount = 0 ;}

I recently spent some time writing the UI. The UI is shown below. It is not easy to read. Please forgive me ~~

Source codeAnd install file download: http://download.csdn.net/detail/xujinsmile/5175683

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.