China Naming Network - Eight-character lottery - Write a Java program, generate a random number from 0 to 9 at the beginning, and win the prize when the difference between the 100 and 10 of the user's membership number is equal to this random number.

Write a Java program, generate a random number from 0 to 9 at the beginning, and win the prize when the difference between the 100 and 10 of the user's membership number is equal to this random number.

//Pack and import java.util.random;

Public static void main(String[] args) {

Random Random = new Random(); //Random number class

int number = random . nextint( 10); //Randomly generate numbers from 0 to 9.

int user number = 0; //A variable that stores the user number

Boolean symbol = false; //Flag whether to cycle.

While (sign == false) {// loop

user number = random . nextint( 1000); //Randomly generate numbers from 0 to 999.

sign = checkUserNum(userNumber,number); //Call the method

}

}

/**

* Check whether the user has won the prize.

* @param userNumber randomly generated user number.

* @param number lottery number

* @return returns yes or no.

*/

public static boolean check usernum(int user number,int number) {

Boolean symbol = false; //Whether winning the prize is marked.

Int firstNum// declares that a variable records hundreds of bits.

Int nextNum// declares that a variable records ten bits.

String buff = new integer (userNumber). toString(); //Convert the user number into a string.

If (buff.length() == 3) {// Judge whether the number of users generated is greater than 100.

first num = integer . parse int(buff . substring(0, 1)); //Intercept hundreds of bits

nextNum = integer . parse int(buff . substring( 1,2)); //Intercept ten digits

If (firstnum-nextnum = = NUMBER) {//If 100 digit minus 10 digit equals the lottery number, the prize will be won.

System.out.println ("lottery number is"+number);

System.out.println ("winning user number is"+buff);

Symbol = true;

}

}

Return flag; //Return

}

Running results:

The lottery number is: 2

The winning user number is: 427

The lottery number is: 7

The winning user number is: 8 16.