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