In: Computer Science
Java code
TIA
I need this: Your program makes accommodations for repeating digits. For example if the random numbers generated were 141 in that order. Then the user entered 271 in that order, be sure that the last one does not count a match to the first and third numbers of the random numbers. They only matched one number in this case. Now if the random numbers are 141 in that order and the user enters 113, then they did match 2 numbers. If they enter 114 then they match 3 numbers but not in order.
public class lotteryGame {
//declaring varibales
static int a,b,c,n1,n2,n3,matchCount=0;
static Scanner input = new Scanner (System.in);
public static void matchNumbers(int a,int b,int c) {
Random random=new Random();
//generating three random numbers (between 0 to 10) and storing it
in n1,n2 and n3
n1= random.nextInt(10);
n2= random.nextInt(10);
n3= random.nextInt(10);
//comapring three random numbers with user entered numbers
if(n1==a)
matchCount++;
if(n2==b)
matchCount++;
if(n3==c)
matchCount++;
//checking how many numbers matched
if(matchCount==1){
System.out.println("\nYou guessed 1 number correctly");
System.out.println("\n***Congratulations you have won
$10000***");
}
else if(matchCount==2){
System.out.println("\nGOOD!!,You guessed 2 numbers
correctly");
System.out.println("\nCongratulations you have won $100000");
}
else if(matchCount==3){
System.out.println("\nAmazing!!,You guessed all 3 numbers
correctly");
System.out.println("\n***Congratulations you have won
$1000000***");
}
else
{
System.out.println("\nYou guessed all 3 numbers
in-correctly");
System.out.println("-1");
}
}
public static void main(String args[])
{
//printing welcome message and also the amount for correct
guesses
System.out.println("********WELCOME TO LOTTERY
GAME********");
System.out.println("GAME PRICES: ");
System.out.println("FOR ALL THREE CORRECT MATCHES:
$1000000");
System.out.println("FOR TWO CORRECT MATCHES: $100000");
System.out.println("FOR ONLY ONE CORRECT MATCHES: $10000");
System.out.println("FOR ALL THREE IN-CORRECT MATCHES: it will show
-1");
//reading three numbers from users
System.out.println("\nEnter three numbers(0<=numbers<10):
");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
//calling method matchNumbers() by passing three parameters
matchNumbers(a,b,c);
}
}
//Changes made according to the requirement.
import java.util.*;
public class lotteryGame {
//declaring varibales
static int a,b,c,n1,n2,n3,matchCount=0;
static Scanner input = new Scanner (System.in);
public static void matchNumbers(int a,int b,int c) {
Random random=new Random();
//generating three random numbers (between 0 to 10) and storing it in n1,n2 and n3
n1= random.nextInt(10);
n2= random.nextInt(10);
n3= random.nextInt(10);
//comapring three random numbers with user entered numbers
//Modified
if(n1==a)
{
matchCount++;
n1=99; //changed it to a number above 10 so that it do not match again and again.
}
else if(n1==b)
{
matchCount++;
n1=99;
}
else if(n1==c)
{
matchCount++;
n1=99;
}
if(n2==a)
{
matchCount++;
n2=99;
}
else if(n2==b)
{
matchCount++;
n2=99;
}
else if(n2==c)
{
matchCount++;
n2=99;
}
if(n3==a)
{
matchCount++;
n3=99;
}
else if(n3==b)
{
matchCount++;
n3=99;
}
else if(n3==c)
{
matchCount++;
n3=99;
}
//checking how many numbers matched
if(matchCount==1){
System.out.println("\nYou guessed 1 number correctly");
System.out.println("\n***Congratulations you have won $10000***");
}
else if(matchCount==2){
System.out.println("\nGOOD!!,You guessed 2 numbers correctly");
System.out.println("\nCongratulations you have won $100000");
}
else if(matchCount==3){
System.out.println("\nAmazing!!,You guessed all 3 numbers correctly");
System.out.println("\n***Congratulations you have won $1000000***");
}
else
{
System.out.println("\nYou guessed all 3 numbers in-correctly");
System.out.println("-1");
}
}
public static void main(String args[])
{
//printing welcome message and also the amount for correct guesses
System.out.println("********WELCOME TO LOTTERY GAME********");
System.out.println("GAME PRICES: ");
System.out.println("FOR ALL THREE CORRECT MATCHES: $1000000");
System.out.println("FOR TWO CORRECT MATCHES: $100000");
System.out.println("FOR ONLY ONE CORRECT MATCHES: $10000");
System.out.println("FOR ALL THREE IN-CORRECT MATCHES: it will show -1");
//reading three numbers from users
System.out.println("\nEnter three numbers(0<=numbers<10): ");
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
//calling method matchNumbers() by passing three parameters
matchNumbers(a,b,c);
}
}