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: