-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathLotteryLabelledContinue.java
executable file
·51 lines (34 loc) · 1.12 KB
/
LotteryLabelledContinue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package tutorialquestions.question4c70;
import java.util.Random;
class LotteryLabelledContinue {
private static final int numNumbers = 7;
private static final int lotteryMax = 49;
/**
* This solution illustrates the use of a labelled `continue'. This is
* not commonly used in Java, and is considered bad practice by some,
* but here it is!
*/
public static void main(String[] args) {
int[] numbers = new int[numNumbers];
Random generator = new Random();
int nextNumber = 0;
chooseNumbers:
while (nextNumber < numNumbers) {
int candidateNumber = generator.nextInt(lotteryMax) + 1;
for (int i = 0; i < nextNumber; i++) {
if (numbers[i] == candidateNumber) {
// The number we chose is no good: skip on to next outer loop iteration.
continue chooseNumbers;
}
}
numbers[nextNumber] = candidateNumber;
nextNumber++;
if (nextNumber < numNumbers) {
System.out.print("Number " + nextNumber);
} else {
System.out.print("Bonus ball");
}
System.out.println(": " + candidateNumber);
}
}
}