Java take random number, stone scissors cloth Example

Source: Internet
Author: User

One, take random number:

 import  java.util.Random; //  import random number  public  class   test{ public  static  void   Main (string[] args) {Random xx  = new Random (); //         declare random number     int  number = Xx.nextint (10); //         assigning random numbers to number         System.out.println ("Random number:" +number); //  output random number   

Second, stone scissors cloth game, Example:

ImportJava.util.Scanner;//Import ScannerImportJava.util.Random;//Import Random Number Public classtest{ Public Static voidMain (string[] args) {//user InputScanner in =NewScanner (system.in); System.out.println ("Please enter your order: \ n If the stone is out, enter the 0\n if the scissors please enter the 1\n if the cloth please enter 2"); intuser =In.nextint (); //System GenerationRandom xx =NewRandom ();//declaring random numbers        intNumber = Xx.nextint (3);//assigning random numbers to number//Compare        if(User==0 && number==0) {System.out.println ("You're out of the stone, the system is stone, the two sides draw. "); }        Else if(User==1 && number==0) {System.out.println ("You have scissors, the system is a stone, you lose." "); }        Else if(user==2 && number==0) {System.out.println ("You are out of the cloth, the system is out of the stone, you won." "); }        Else if(User==0 && Number==1) {System.out.println ("You have a stone, the system is a pair of scissors, you win." "); }        Else if(User==1 && Number==1) {System.out.println ("You're out of the stone, the system is stone, the two sides draw. "); }        Else if(user==2 && Number==1) {System.out.println ("You are out of cloth, the system is a pair of scissors, you lose." "); }        Else if(User==0 && number==2) {System.out.println ("You are a stone, the system is a cloth, you lose." "); }        Else if(User==1 && number==2) {System.out.println ("You have scissors, the system is a cloth, you win." "); }        Else{System.out.println ("You are out of cloth, the system is out of cloth, both sides draw. "); }    }}

Operation Result:

Java take random number, stone scissors cloth Example

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.