public class App {
public static void main( String[] args ) {
int[] set = {0,0,0,0,0,0,0};
for(int i = 0; i < 7; i++) {
boolean unique = false;
while (!unique) {
int j = 0;
try {
j = JRandom.randInt(1, 45);
}
catch (ParamterException e) {
e.printStackTrace();
}
unique = true;
for(int k = 0 ; k < 7; k++) {
if (j==set [k]) {
unique = false;
break;
}
}
if (unique) {
set [i] = j;
}
}
}
for (int i= 0; i < 6; i++) {
if (i == 5) {
System.out.println(set[i]);
}
else {
System.out.print(set[i]+", ");
}
}
System.out.println("Bonus Ball = " + set[6]);
}
}
I was wondering how I would implement bubble-sort into this code. It works on my machine and produces 6 random numbers + a bonus ball.
2, 34, 25, 14, 39, 13
Bonus Ball = 30
I was aiming to make it print so the numbers are ascending so that it would be like the lotto, the idea anyway.
That's all, thanks.
Can make like this:
for (int i = set.length-1; i >= 0; i --) {
for (int j = 0; j < i; j++) {
if (set[j] > set[j + 1]) {
int aux = set[j];
set[j] = set[j + 1];
set[j + 1] = aux;
}
}
}
The initial loop, in this way also helps, not to waste resources comparing indexes that have already been sorted.
If the use of bubble sort algorithm is not mandatory, just use the Array.sort method already mentioned.