Current location - Training Enrollment Network - Books and materials - JAVA course design lottery number generator
JAVA course design lottery number generator
I output a super lottery code on the console. I hope I can help you: 1. Say the idea first. A. Declare that two arrays and two variable arrays are placed in the front area of 35 and the back area of 12 respectively. The variable is the length of two arrays. B, firstly, randomly obtain five numbers in the front area: random. NextInt (a variable with the length of the front area) gets the random number, finds the element whose subscript is this random number in the first array, and takes it out and puts it into the variable. This can judge the format. Regular expressions can be used to judge whether it is a number of numbers. If yes, please fill in 0. Then remove the element from the array. Of course, the variables that store the array should be subtracted from 1 and then repeated four times, thus obtaining five non-repeated numbers in 1-35. C. same as above. 2. Super happy with the code. Java imports Java. Util。 Arraylist

Import java.util.arrays;

Import java.util.list;

Import java.util.random;

Import java.util.regex.matcher;

Import java.util.regex.pattern; /**

* The following is a simulation of a super lottery random number selection function, which is generated in strict accordance with the probability.

* @ Author Jack

*

*/

Open class is super happy {

//Declare a list to store front area numbers.

Private distribution list & lt integer & gtobjfrontlist = new ArrayList & lt integer & gt ();

//Declare a list to store area codes.

Private distribution list & lt integer & gtobjBackList = new ArrayList & lt integer & gt ();

//Declare a regular expression object to match whether it is a single digit or not, and use it for output verification.

pattern objP = pattern . compile(" \ \ d ");

//The string to be operated on

Matcher objM = null

String[]Front = new String[5];

String[] Back = new string [2]; //Initialize the lottery number

public void init() {

for(int I = 1; I<= 35; i++) {

objfrontlist . add(I);

}

for(int I = 1; I<= 12; i++) {

objbacklist . add(I);

}

}//Start the lucky draw.

public void beginLottery() {

Random obj Random = new Random();

Int nFrontCount = 35// Total number of front zone numbers

int nback count = 12; //Total number of back area numbers//Clear the list before the lottery, and then initialize it.

objfrontlist . clear();

//system . out . println(objFrontList);

objbacklist . clear();

//system . out . println(objBackList);

this . init();

/**

* generate 5 front area numbers.

*/

for(int I = 0; I<5; i++) {

//system . out . println(" nFrontCount:"+nFrontCount);

//Initially there are 35 prefix codes, and an index is randomly generated.

int nIndex = obj random . nextint(nFrontCount);

//Temporarily store the selected number in a variable with regular expression validation.

int nTemp = objfrontlist . get(nIndex);

String strTemp = new integer (nTemp). toString();

//Match the obtained number with a regular expression

objM = objp . matcher(strTemp);

Boolean flag = objm. matches ();

//If it is a single digit, zero should be added.

If (logo) {

front[I]=(" 0 "+strTemp+" ");

} Otherwise {

front[I]=(strTemp+" ");

}

//Delete the number at this index in the list, because if one is selected, it will not be put back.

objfrontlist . remove(nIndex);

//The total number of figures is reduced by one.

nFrontCount-;

}

Arrays.sort (front end);

for(int n = 0; N < front. length; n++) {

system . out . print(Front[n]+" \ t ");

}

system . out . print("+");

/**

* generate 2 back area numbers.

*/

for(int I = 0; I<2; i++) {

//system . out . println(" nBackCount:"+nBackCount);

//Initially, there are 12 area codes, and an index is generated randomly.

int nIndex = obj random . nextint(nBackCount);

//Temporarily store the selected number in a variable with regular expression validation.

int nTemp = objbacklist . get(nIndex);

String strTemp = new integer (nTemp). toString();

//Match the obtained number with a regular expression

objM = objp . matcher(strTemp);

Boolean flag = objm. matches ();

//If it is a single digit, zero should be added.

If (logo) {

back[I]=(" 0 "+strTemp+" ");

} Otherwise {

back[I]=(strTemp+" ");

}

//Delete the number at this index in the list, because if one is selected, it will not be put back.

objbacklist . remove(nIndex);

//for(int n = 0; n & ltobjbacklist . size(); n++){

//system . out . println(" obj backlist:"+obj backlist . get(n));

// }

//The total number of figures is reduced by one.

nback count-;

}

arrays . sort(Back);

for(int n = 0; N & ltBack. length; n++) {

system . out . print(Back[n]+" \ t ");

}

//Take notes and press enter.

system . out . println(" ");

}//How many lottery tickets are output as required?

public void output lottery(int VN count){

for(int I = 0; I & ltvncounti++)

this . begin lottery();

}

} /**

* @param args

*/

Public static void main(String[] args) {

SuperJoyful objSJ = new SuperJoyful

enter console objEC = new enter console();

//Declare a regular expression object to match whether it is a number.

pattern objP = pattern . compile(" \ \ d { 1,} ");

//The string to be operated on

Matcher objM = null

//Receive console input

String objTemp =(String)objec . print console();

//String strTemp =(String)objTemp;

objM = objp . matcher(objTemp);

Boolean flag = objm. matches ();

int nTemp = 0;

If (logo) {

NTemp = new integer (objtemp);

} Otherwise {

System.out.println ("Sorry, only numbers can be entered!" );

}

objsj . output lottery(nTemp);

}

} enter console . javaimport Java . io . buffered reader;

Import java.io.ioexception;

Import java.io.inputstreamreader;

Common class input console

{

Public object printConsole(){

System.out.print ("Please enter the number of notes you want to randomly select and press Enter:");

buffered reader objBR = new buffered reader(new InputStreamReader(system . in));

String strValue = null

attempt

{

strValue =(String)objbr . readline();

}

IOException e

{

// TODO automatically generated catch block

e . printstacktrace();

}

Return to strValue

}

}