-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLotteryUsingStrings.java
35 lines (28 loc) · 1.39 KB
/
LotteryUsingStrings.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
import java.util.Scanner;
public class LotteryUsingStrings {
public static void main(String[] args) {
// Generate a lottery as two digit string
String lottery = "" + (int)(Math.random() * 10) + (int)(Math.random() * 10);
System.out.printf("%-8d%-8s%-8.1f\n", 1234, "Java", 5.63);
// Prompt the user to enter a guess
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (two digits): ");
String guess = input.nextLine();
// Get digits from lottery
char lotteryDigit1 = lottery.charAt(0);
char lotteryDigit2 = lottery.charAt(1);
// Get digits from guess
char guessDigit1 = guess.charAt(0);
char guessDigit2 = guess.charAt(1);
System.out.println("The lottery number is " + lottery);
// check the guess
if(guess.equals(lottery))
System.out.println("Exact match: You win $10,000");
else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2)
System.out.println("match all digits: you win $3,000");
else if(guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2)
System.out.println("Match one digit: you win $1,000");
else
System.out.println("Sorry, no match please play more to win");
}
}