- Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathLotteryLabelledContinue.java
More file actions
Latest commit
executable file
·51 lines (34 loc) · 1.12 KB
/
Copy pathLotteryLabelledContinue.java
File metadata and controls
executable file
·51 lines (34 loc) · 1.12 KB
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
packagetutorialquestions.question4c70;
importjava.util.Random;
classLotteryLabelledContinue {
privatestaticfinalintnumNumbers = 7;
privatestaticfinalintlotteryMax = 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!
*/
publicstaticvoidmain(String[] args) {
int[] numbers = newint[numNumbers];
Randomgenerator = newRandom();
intnextNumber = 0;
chooseNumbers:
while (nextNumber < numNumbers) {
intcandidateNumber = generator.nextInt(lotteryMax) + 1;
for (inti = 0; i < nextNumber; i++) {
if (numbers[i] == candidateNumber) {
// The number we chose is no good: skip on to next outer loop iteration.
continuechooseNumbers;
}
}
numbers[nextNumber] = candidateNumber;
nextNumber++;
if (nextNumber < numNumbers) {
System.out.print("Number " + nextNumber);
} else {
System.out.print("Bonus ball");
}
System.out.println(": " + candidateNumber);
}
}
}