Write a Java program that determines if a person hits the \"Pick 3\" loterry num
ID: 670977 • Letter: W
Question
Write a Java program that determines if a person hits the "Pick 3" loterry numbers.
Have the program to randomly create a 3 digit lottery number.
Have the user enter the number they will be playing and the results: (DO NOT USE ARRAYS or STRINGS)
-If the user input matches the lottery number in the exact order, the award is $5Million.
-If all the digits in the user input match all the digits in the lottery number, the award is $500,000.
-If one digit in the user input matches a digit in the lottery number, the award is $5,000.
-If nothing matches, inform the person, “Sorry, try again!”.
BOOK USED: Java How to Program, 10th Edition, by Paul Deitel and Harvey Deitel, Publisher: Prentice Hall (Pearson).
THIS IS NOT AN ASSIGNMENT ONLY PRACTICE!!!
Explanation / Answer
import java.util.*;
import java.util.Scanner;
class RandomNumbers {
public static void main(String[] args) {
int c,ans,guess,count;
int a[] = new int[11];
Random r = new Random();
count =0;
// random integers in [0, 100]
// for (c = 1; c <= 10; c++) {
ans= r.nextInt(900)+100;
Scanner in = new Scanner (System.in);
System.out.println("enter your guess (three digit number) ");
do
{
guess = in.nextInt();
if(guess < 100 || guess >999)
System.out.println("Enter a three digit number");
}
while(guess < 100 || guess > 999);
if(ans == guess)
{
System.out.println("You won $5 millon");
}
else
{
while(ans>0)
{
a[ans%10]++;
ans/=10;
}
while(guess>0)
{
if(a[guess%10]>0)
{
a[guess%10]--;
count++;
}
guess/=10;
}
if(count == 3)
System.out.println("you won $500,000");
else if(count > 0)
System.out.println("you won $5000");
else
System.out.println("Btter luck next time");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.