The lottery program involves generating random numbers, comparing digits, and using Boolean operators. Suppose you want to develop a program to play lottery. The program randomly generates a lottery of a two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is $10,000.
2. If all digits in the user input match all digits in the lottery number, the award is $3,000.
3. If one digit in the user input matches a digit in the lottery number, the award is $1,000. Note that the digits of a two-digit number may be 0.
If a number is less than 10, we assume the number is preceded by a 0 to form a two-digit number. For example, number 8 is treated as 08 and number 0 is treated as 00 in the program.
Output: Test Cases
Case-1 Enter your lottery pick (two digits): 15 The lottery number is 15 Exact match: you win $10,000
Case-2 Enter your lottery pick (two digits): 45 The lottery number is 54 Match all digits: you win $3,000
Case-3 Enter your lottery pick: 23 The lottery number is 34 Match one digit: you win $1,000
Case-4 Enter your lottery pick: 23 The lottery number is 14 Sorry: no match
import java.util.Random;
import java.util.Scanner;
public class java7
{
public static void main(String[] args)
{
int number, lottery;
Scanner sc = new Scanner(System.in);
Random rand = new Random();
lottery = rand.nextInt(100);
System.out.print("Enter your lottery pick (two digits) => ");
number = sc.nextInt();
if(lottery == number)
{
System.out.println("Exact match: you win $10,000");
}
else
{
int d1, d2, l1, l2;
d1 = number % 10;
number = number / 10;
d2 = number % 10;
l1 = lottery % 10;
lottery = lottery / 10;
l2 = lottery % 10;
intcnt = 0;
if(d1 == l1 || d1 == l2)
cnt++;
if(d2 == l1 || d2 == l2)
cnt++;
if(cnt == 0)
System.out.println("Sorry: no match");
else if(cnt == 1)
System.out.println("Match one digit: you win $1,000");
else if(cnt == 2)
System.out.println("Match all digits: you win $3,000");
}
}
}
Output: Enter your lottery pick(two digits)==>39
Sorry : No match