PackageLianxi;ImportJava.util.Random; Public classSuijishu { Public Static voidMain (string[] args) {random random=NewRandom (); int[] arr =New int[4]; arr[0] = Random.nextint (9); inti = 1; //Outer Loop definition four number while(I <=3) { intx = Random.nextint (9); /*Inner Loop: The new generated random number and the generated comparison, * the same will jump out of the inner loop, regenerated into a random number for comparison * and the previous generation is different then this is the new random number*/ for(intj = 0; J <= I-1; J + +) { //the same will jump out of the inner loop and regenerate into a random number to compare if(Arr[j] = =x) { Break; } //after executing the loop and the first few builds are different then this is the new random number if(j+1==i) {Arr[i]=x; I++; } } } //print out the generated random number for(intAaa:arr) {System.out.print (AAA); } }}
Java generates 4 different random numbers