Home > Back-end >  The output of the red ball sometimes repetitive, how to use the bubble sort method will be raised to
The output of the red ball sometimes repetitive, how to use the bubble sort method will be raised to

Time:10-07

Package shuzu;

import java.util.Random;

/* *
* 1 implement the corresponding requirement: the double chromosphere results generated
The double chromosphere have 2 number in the ball, the front is a red ball, a total of 33, requires 6 not repeat results randomly generated; Followed by a blue ball, blue ball is 16, randomly generated 1
Generate the corresponding results in accordance with the requirements, and the red ball order by size and output overall results (requirements: if the number is less than 10, the front of the output to fill 0)
Output (example: the red ball: 02 07 11 22 25 blue ball: 33 09)
* @ author Administrator
*
*/

Public class Test3_16_1 {

Public static void main (String [] args) {
System. The out. Println (" red ball: ");
Int red []=new int [6].

For (int I=0; I & lt; Red. Length; I++) {
Red [I]=(int) (Math. The random () * 32 + 1);


/* for (int j=0; JFor (int k=0; K & lt; Red. The length - 1 - j; K++) {
If (red [k] & gt; Red [k + 1]) {
Int temp=red [k + 1);
Red [k + 1)=red [k].
Red [k]=temp;
}

}

} */
If (red [I] <10) {
System. The out. Println (" 0 "+ red [I]);
} else {

System. The out. Println (" "+ red [I]);
}

}

System. The out. Println (" blue: ");
Int bule []=new int [1].

For (int i1=0; I1 & lt; Bule. Length; {i1 + +)
Bule [i1]=(int) (Math. The random () * 16);
If (bule [i1] <10) {
System. The out. Println (" 0 "+ bule/i1);
} else {

System. The out. Println (" "+ bule/i1);
}

}

}

}


CodePudding user response:

Red ball array [33] 1, take out randomly, arrays are removed out number, so that will not repeat

CodePudding user response:

 package test. Lt20; 

Import the Java. Util. Arrays;

Public class Test09 {

Public static void main (String [] args) {
//TODO Auto - generated method stub

Int [] ar=new int [33].
for(int i=0; I & lt; 33. I++) {
Ar [I]=I + 1;
}
for(int i=0; I & lt; 6; I++) {
Int independence idx=(int) (Math. The random () * (33 - I));
Int temp=ar -i] [33-1;
Ar -i] [33-1=ar [independence idx];
Ar/independence idx=temp;
}

Int [] ar2=new int [6].
System. Arraycopy (ar, 33-6, ar2, 0, 6);

The Arrays. Sort (ar2);

For (int I: ar2) {
02 System. Out. Printf (" % d \ t ", I);
}
}

}

CodePudding user response:

This is another way, I tried

CodePudding user response:

refer to the second floor nayi_224 response:
 package test. Lt20; 

Import the Java. Util. Arrays;

Public class Test09 {

Public static void main (String [] args) {
//TODO Auto - generated method stub

Int [] ar=new int [33].
for(int i=0; I & lt; 33. I++) {
Ar [I]=I + 1;
}
for(int i=0; I & lt; 6; I++) {
Int independence idx=(int) (Math. The random () * (33 - I));
Int temp=ar -i] [33-1;
Ar -i] [33-1=ar [independence idx];
Ar/independence idx=temp;
}

Int [] ar2=new int [6].
System. Arraycopy (ar, 33-6, ar2, 0, 6);

The Arrays. Sort (ar2);

For (int I: ar2) {
02 System. Out. Printf (" % d \ t ", I);
}
}

}

Arraycopy?
  • Related