Java guessing numbers game

Source: Internet
Author: User

I. Requirements: When the program is running

1. Prompt the user to enter a number

2. If you guessed, the output guessed right.

3. If you do not guess the number is large or small, and prompts to continue guessing until the Guess

4. User press Enter 0 to exit the game

5. Write with a while loop

6. The guessed numbers are randomly generated between 1 and 100


Two. Code

import java.util.scannerclass guessing{    public static void  Main () {        scanner sc = new scanner (System.in) ;        int num =  (int) (Math.random () *100+1);         system.out.println ("Please enter")          int guess = sc.nextint ();         while (Guess!=num ) {            if (guess==0) {                 system.out.println ("Exit Game");                 break;             }             if (guess>num) {                 system.out.println ("Big Up");            }             if (Guess<num) {                 system.out.println ("small");             }             if (Guess==num) {                 sytem.out.println ("guessed right");                 break;             }            system.out.println ("Please enter")     &nBsp;       gusee = sc.nextint ();         }    }}

In fact, there are many ways to write, I only wrote one of them, if there are mistakes or suggestions want to put forward, I hope you can send a message or messages thank you.

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.