Demand:
Gets 10 random numbers between 1-20 and requires no repetition
Analysis:
A: Create an object that produces a random number
B: Create a collection to store the resulting random number
C: Determine the random number:
A: Define a count to determine if the number of random numbers is less than 10
1: If less than 10, then the second judgment
Whether this random number exists: if it does not exist, then add it, if present, ignore
2: If it is greater than or equal to 10, exit
D: Traversal output of the collection
1 Packagecn_jdk5new;2 3 Importjava.util.ArrayList;4 Importjava.util.Random;;5 Public classRandomtest {6 7 Public Static voidMain (string[] args) {8 9 //create an object that produces a random numberTenRandom r =NewRandom (); One A //create a collection to store random numbers -arraylist<integer> list =NewArraylist<integer>(); - the //Judging random numbers - intCount = 0; - while(Count < 10){ - intNumber = R.nextint (20) + 1;//generates a random number between 1-20 + if(!list.contains (number)) {//determine if a random number exists - List.add (number); +Count + +; A } at } - //iterating through the collection - for(Integer i:list) { - System.out.println (i); - } - in } - to}
Java 16-14 establishes a set of random numbers that produce a finite number of repetitions