Guess a number game written in Java (interesting)

Source: Internet
Author: User

Finally, I came into contact with Java. Java is really different from C, and the design idea is completely different.

Now I have experienced the amazing experience in object programming. I found that I have fallen in love with this programming language.

Two days ago, I had nothing to do with writing a Java guess digital game. In fact, I thought about it for a long time, but it has never been put into practice.

Just recently I just learned Java and wrote a game to train my trainer.

I remember that in junior high school, I played this game in the Learning Machine of wenquxing.

Generate four random numbers (0 ~ Between 9 and not repeated), and then the player guesses. A total of 6 opportunities

Every time you guess, the computer prompts players

If the number to be guessed is incorrect, the number a is displayed.

If the number to be guessed is the same and the corresponding position is the same, the number of B is displayed.

For example, the number to be guessed is 0713.

If the player enters 2045

1a0b is displayed. The number is 0 but the position is incorrect.

Players enter 0231 again

Display 2a1b

 

 

After all, this is the first interesting Java program I have written.

Guessdigit class (generates four random numbers that are not repeated)

Package guess number; import Java. util. *; public class guessdigit {private string digit = ""; Private Static final int size = 4; Public String getdigit () {random r = new random (); int n = 0; while (n <size) {string temp = ""; int I = R. nextint (10); temp + = (char) ('0' + I); If (! Digit. contains (temp) {digit + = (char) ('0' + I); N ++;} return digit;} public int getsize () {return size ;}}

Guessresult class (return the computer prompt result)

Package guess number; public class guessresult {private int A = 0; // The number of different positions with the same number. Private int B = 0; // number: guessdigit G = new guessdigit (); int size = G. getsize (); Public void getresult (string guessdigit, string digitin) {A = 0; B = 0; For (INT I = 0; I <size; I ++) {If (digitin. charat (I) = guessdigit. charat (I) {B ++ ;}}for (Int J = 0; j <size; j ++) {for (int K = 0; k <size; k ++) {If (digitin. charat (j) = guessdigit. charat (k) {// prevent repeated requests to a ++ A ++; break ;}}} A-= B; system. out. println (a + "A" + B + "B");} public int getb () {return B ;}}

Guessdigittester class (player testing class)

Package guess number; import Java. util. *; public class guessdigittester {/*** @ Param ARGs */public static void main (string [] ARGs) {system. out. println ("you have 6 chances! Enter four (0-9) numbers each time "); system. out. println ("A indicates that the number is included but the position is incorrect. B indicates that the number position is correct."); system. out. println ("good luck! "); Int COUNT = 6; int I = 0; string digitin; string guessdigit; Limit SC = new limit (system. in); guessresult G = new guessresult (); guessdigit H = new guessdigit (); guessdigit = H. getdigit (); for (I = 0; I <count; I ++) {digitin = SC. next (); If (digitin. length ()! = H. getsize () {system. out. println ("Enter four numbers:"); continue;} G. getresult (guessdigit, digitin); If (G. getb () = H. getsize () {system. out. println ("You guessed it! ");} Else {Int J = count-i-1; system. out. println ("You still have" + J + "") ;}} if (I >= count) {system. out. println ("sorry, this number is" + guessdigit); // H. getdigit (), which will re-initialize the number to guess }}}

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.