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