-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlotto.java
More file actions
67 lines (48 loc) · 1.8 KB
/
lotto.java
File metadata and controls
67 lines (48 loc) · 1.8 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
//play the lottery game with the user
//read the lottery numbers and print
//matching lottery ticket numbers
public class Lottery {
public static final int NUMBERS =6;
public static final int MAX_NUMBERS = 10;
public static void main(String[] args) {
//get the winning number and tickets sets
Set<Integer> winningNumbers = createWinningNumbers();
Set<Integer> ticket = getTicket();
Set<Integer> intersection = new TreeSet<Integer>(ticket);
intersection.retainAll(winningNumbers);
System.out.println(" Your ticket numbers are " + ticket);
System.out.println(" The winning numbers are " + winningNumbers);
System.out.println(" You had " + intersection.size()+ " matching number");
if(intersection.size()>0) {
double prize = 100 * Math.pow(2,intersection.size());
System.out.println(" The Matched numbers are " + intersection);
System.out.println(" Your Prize is $" + prize);
}
}
//getnerate a set of winning lotto numbers
public static Set<Integer> createWinningNumbers(){
Set<Integer>winningNumbers = new TreeSet<Integer>();
Random r = new Random();
while ( winningNumbers.size()<NUMBERS){
int number = r.nextInt(MAX_NUMBERS + 1);
winningNumbers.add(number);
}
return winningNumbers;
}
//Get Tickets // read the players lottery ticket from the console
public static Set<Integer> getTicket(){
Set<Integer> ticket = new TreeSet<Integer>();
Scanner console = new Scanner(System.in);
System.out.print("Enter your " + NUMBERS + " unique lotto numbers:");
while (ticket.size()< NUMBERS) {
int number = console.nextInt();
ticket.add(number);
}
console.close();
return ticket;
}
}