java-guessing numbers game

Source: Internet
Author: User

Summary: Games involve game and player two objects, game will produce random numbers between 0-9

Program logic:

    1. Gamelauncher This class has the main () method, which is the entry point of the application
    2. The Guessgame object is created in main () and her Startgame () method is called
    3. The Startgame () method is the starting point of the game, it creates 3 players, and then picks out the number of guesses, which will ask the player to guess and check the results, and the process will be listed.

Class:

    • Gamelauncher.class
    • Guessgame.class
    • Player.class

Gamelauncher.class

1  Public class Gamelauncher {2      Public Static void Main (string[] args) {3         New guessgame (); 4         game.startgame (); 5     }6 }

Guessgame.class

1  Public classguessgame{2     //3 player objects represented by 3 instance variables3 Player p1;4 Player p2;5 Player P3;6      Public voidStartgame () {7         //Create a Player object8p1=NewPlayer ();9P2 =NewPlayer ();Tenp3=NewPlayer (); One         //define 3 variables to save guessing A         intGUESSP1 = 0; -         intGUESSP2 =0; -         intGUESSP3 = 0; the         //define 3 variables to hold the guessed number -         BooleanP1isright =false; -         BooleanP2isright =false; -         BooleanP3isright =false; +         intTarnum = (int) (Math.random () *30);//Generate mystery Numbers -System.out.println ("I guess 1 to 30"); +          while(true) A         { atSystem.out.println ("Number to guess is" +tarnum); -P1.guess ();//call The Guess method in the player - p2.guess (); - p3.guess (); -             //make a list of the numbers each player guesses -GUESSP1 =P1.number; inSystem.out.println ("one" +guessp1); -GUESSP2 =P2.number; toSystem.out.println ("both" +guessp2); +guessp3=P3.number; -System.out.println ("three" +guessp3); the             //Check if you guessed, *             if(guessp1==tarnum) $             {Panax Notoginsengp1isright=true; -             } the             if(guessp2==tarnum) +             { Ap2isright=true; the                 } +             if(guessp3==tarnum) -             { $p3isright=true; $             } -             //If you guessed one or more ... -             if(P1isright| | p2isright| |p3isright) the             { -System.out.println ("We are a winner");WuyiSystem.out.println ("one" +p1isright); theSystem.out.println ("both" +p2isright); -System.out.println ("three" +p3isright); WuSystem.out.println ("Game is Over");  -                  Break;//because the above while is true, it is executed until the IF statement is true and the following break does not end the loop About         } $         Else  -{System.out.println ("123");} -     }     - } A}

Player.class

1  Public class Player {2     int number = 0;  // the number to be guessed 3      Public void guess () {4number         = (int ) (Math.random () *30); 5         SYSTEM.OUT.PRINTLN ("I guessing" + number); 6     }7     }

java-guessing numbers game

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.