JAVA random number, stone scissors cloth instance, java Random Number

Source: Internet
Author: User

JAVA random number, stone scissors cloth instance, java Random Number

1. Random Number retrieval:

Import java. util. random; // import the Random number public class Test {public static void main (String [] args) {Random xx = new Random (); // declare the Random number int number = xx. nextInt (10); // assign a random number to number System. out. println ("random number:" + number); // output random number }}

Ii. Stone scissors cloth game, example:

Import java. util. scanner; // import the Scanner import java. util. random; // import Random number public class Test {public static void main (String [] args) {// user input Random in = new Random (System. in); System. out. println ("Enter the one you want to output: \ n. If the stone is exported, enter 0 \ n. If the scissors are exported, enter 1 \ n. If the scissors are exported, enter 2 "); int user = in. nextInt (); // The system generates Random xx = new Random (); // declares the Random number int number = xx. nextInt (3); // assign a random number to number // compare if (user = 0 & number = 0) {System. out. println ("you are a stone, System The two sides drew a draw. ");} Else if (user = 1 & number = 0) {System. out. println ("You are out of scissors, the system is out of stones, you are lost. ");} Else if (user = 2 & number = 0) {System. out. println ("you are out of the cloth, the system is out of the stone, you win. ");} Else if (user = 0 & number = 1) {System. out. println ("you are a stone, the system is a scissors, you win. ");} Else if (user = 1 & number = 1) {System. out. println ("you are out of the stone, the system is out of the stone, the two sides draw. ");} Else if (user = 2 & number = 1) {System. out. println ("you are out of the cloth, the system is out of the scissors, you are lost. ");} Else if (user = 0 & number = 2) {System. out. println ("you are a stone, the system is a cloth, and you are lost. ");} Else if (user = 1 & number = 2) {System. out. println ("You are out of scissors, the system is out of cloth, you win. ");} Else {System. out. println (" you are out of the cloth, the System is out of the cloth, and both sides are tied. ");}}}

Running result:

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.