Question: In java: import java.util.Scanner; public class lottery { public static void main(String[] args) { int lottery = 719; // Prompt the user to enter a

In java:

import java.util.Scanner;

public class lottery {

public static void main(String[] args) {

int lottery = 719;

// Prompt the user to enter a guess

Scanner input = new Scanner(System.in);

System.out.print("Enter three digit number: ");

int guess = input.nextInt();

// Get digits from lottery

int lotteryDigit1 = lottery / 100;

int lotteryDigit2 = (lottery % 100) / 10;

int lotteryDigit3 = lottery % 10;

// Get digits from guess

int guessDigit1 = guess / 100;

int guessDigit2 = (guess % 100) / 10;

int guessDigit3 = guess % 10;

System.out.println("The lottery number is: " + lottery);

// Sum up both sets of digits to compare for 3 inconsecutive matches

int guessSum = guessDigit1 + guessDigit2 + guessDigit3;

int lotterySum = lotteryDigit1 + lotteryDigit2 + lotteryDigit3;

// Check the guess

if (guess == lottery)

System.out.println("1st prize: three digits match in exact order.");

//

else if((guessDigit1 == lotteryDigit1

|| guessDigit1 == lotteryDigit2

|| guessDigit1 == lotteryDigit3)

&& (guessDigit2 == lotteryDigit1

|| guessDigit2 == lotteryDigit2

|| guessDigit2 == lotteryDigit3)

&& (guessDigit3 == lotteryDigit1

|| guessDigit3 == lotteryDigit2

|| guessDigit3 == lotteryDigit3)

&& guessSum == lotterySum)

System.out.println("2nd prize: three digits match, but not in the exact order.");

else if ((guessDigit1 == lotteryDigit1

|| guessDigit1 == lotteryDigit2

|| guessDigit1 == lotteryDigit3)

|| (guessDigit2 == lotteryDigit1

&& guessDigit2 == lotteryDigit2

|| guessDigit2 == lotteryDigit3)

|| (guessDigit3 == lotteryDigit1

|| guessDigit3 == lotteryDigit2

&& guessDigit3 == lotteryDigit3))

System.out.println("3rd prize: two digits match, in its exact position.");

else if ((guessDigit1 == lotteryDigit1

&& guessDigit1 == lotteryDigit2

|| guessDigit1 == lotteryDigit3)

|| (guessDigit2 == lotteryDigit1

|| guessDigit2 == lotteryDigit2

&& guessDigit2 == lotteryDigit3)

|| (guessDigit3 == lotteryDigit1

&& guessDigit3 == lotteryDigit2

|| guessDigit3 == lotteryDigit3))

System.out.println("4th prize: two digits match, but not in the exact position.");

//

else if ((guessDigit1 == lotteryDigit1

|| guessDigit1 == lotteryDigit2

|| guessDigit1 == lotteryDigit3)

|| (guessDigit2 == lotteryDigit1

&& guessDigit2 == lotteryDigit2

|| guessDigit2 == lotteryDigit3)

|| (guessDigit3 == lotteryDigit1

|| guessDigit3 == lotteryDigit2

|| guessDigit3 == lotteryDigit3))

System.out.println("5th prize: one digit matches in its exact position.");

else if ((guessDigit1 == lotteryDigit1

|| guessDigit1 == lotteryDigit2

|| guessDigit1 == lotteryDigit3)

|| (guessDigit2 == lotteryDigit1

|| guessDigit2 == lotteryDigit2

|| guessDigit2 == lotteryDigit3)

|| (guessDigit3 == lotteryDigit1

|| guessDigit3 == lotteryDigit2

|| guessDigit3 == lotteryDigit3))

System.out.println("6th prize: one digit matches, but not in the exact position.");

else

System.out.println("Sorry, you win nothing.");

}

}

I can get 1st place, 2st place and win nothing. The others I cant get to work.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!