I'm new to Java and im trying to randomize non-repeating numbers for a Bingo game. My code generates unique random numbers from 1 to 75 only if i do not add a break statement, (which i have to do to only get one number at the time). What do I do?
public static void bingoNumber() {
Random random = new Random();
int num;
String u;
String letter = "";
HashSet<Integer> used = new HashSet<>(75);
for (int i = 1; i <= 75; i++){
ball.add(i);
}
while(used.size() > 0) {
num = 1 + random.nextInt(75);
if (used.contains(num)){
used.remove(new Integer(num));
if ( num >= 1 && num <=15){
letter = "B";
}else if (num >= 16 && num <=30){
letter = "I";
}else if (num >= 31 && num <=45){
letter = "N";
}else if (num >= 46 && num <=60){
letter = "G";
}else if (num >= 61 && num <=75){
letter = "O";
}
u = Integer.toString(num);
System.out.print("BINGO CALL IS: " + letter + u + "\n");
break;
}
if (!used.contains(num)){
continue;
}
}
Output:
BINGO CALL IS: B11
BINGO CALL IS: N34
BINGO CALL IS: B4
BINGO CALL IS: G52
BINGO CALL IS: O70
BINGO CALL IS: I28
BINGO CALL IS: O66
BINGO CALL IS: G48
BINGO CALL IS: G58
BINGO CALL IS: O62
BINGO CALL IS: G56
BINGO CALL IS: G59
BINGO CALL IS: I29
BINGO CALL IS: O71
BINGO CALL IS: I25
BINGO CALL IS: I27
BINGO CALL IS: N31
BINGO CALL IS: N43
BINGO CALL IS: I16
BINGO CALL IS: O72
BINGO CALL IS: O61
BINGO CALL IS: G57
BINGO CALL IS: B7
BINGO CALL IS: I26
BINGO CALL IS: I24
BINGO CALL IS: B8
BINGO CALL IS: O74
BINGO CALL IS: G51
BINGO CALL IS: I30
BINGO CALL IS: B14
BINGO CALL IS: B6
BINGO CALL IS: N32
BINGO CALL IS: O64
BINGO CALL IS: I19
BINGO CALL IS: G49
BINGO CALL IS: N42
BINGO CALL IS: N36
BINGO CALL IS: O69
BINGO CALL IS: I18
BINGO CALL IS: G54
BINGO CALL IS: O63
BINGO CALL IS: N44
BINGO CALL IS: G50
BINGO CALL IS: B12
BINGO CALL IS: N41
BINGO CALL IS: N40
BINGO CALL IS: B9
BINGO CALL IS: O68
BINGO CALL IS: B10
BINGO CALL IS: N38
BINGO CALL IS: I21
BINGO CALL IS: N33
BINGO CALL IS: O75
BINGO CALL IS: G55
BINGO CALL IS: N39
BINGO CALL IS: B13
BINGO CALL IS: I20
BINGO CALL IS: G46
BINGO CALL IS: O67
BINGO CALL IS: I17
BINGO CALL IS: N37
BINGO CALL IS: G53
BINGO CALL IS: G60
BINGO CALL IS: B5
BINGO CALL IS: O65
BINGO CALL IS: N45
BINGO CALL IS: O73
BINGO CALL IS: B15
BINGO CALL IS: I22
BINGO CALL IS: B1
BINGO CALL IS: I23
BINGO CALL IS: N35
BINGO CALL IS: B2
The numbers are unique and random but i only want one number at the time (without repeating) not all 75 at once.
Aucun commentaire:
Enregistrer un commentaire