Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help implementing this Java code package hmwk3; import java.util.Scanner; /

ID: 3841291 • Letter: N

Question

Need help implementing this Java code

package hmwk3;

import java.util.Scanner;

/**
* Revise the Lottery.java from self-review part, to
* generate a lottery of a three-digit number. The program
* prompts the user to enter a three-digit number and determines
* whether the user wins according to the following rules:
* If the user input matches the lottery number in the exact order, the award is $10,000.
* If all digits in the user input match all digits in the lottery number, the award is $3,000.
* If one digit in the user input matches a digit in the lottery number, the award is $1,000.
* @author DR
*
*/
public class PE05
{
   public static void main(String[] args)
   {
       // Generate a lottery
   int lottery = (int)(Math.random() * 1000);

   // Prompt the user to enter a guess
   Scanner input = new Scanner(System.in);
   System.out.print("Enter your lottery pick (three digits): ");
   int guess = input.nextInt();

   // Get digits from lottery
   int lotteryDigit1 = lottery / 100;
   int remainingLotteryDigits = lottery % 100;
   int lotteryDigit2 = remainingLotteryDigits / 10;
   int lotteryDigit3 = remainingLotteryDigits % 10;
  

   // Get digits from guess
   int guessDigit1 = guess / 100;
   int remainingGuessDigits = guess % 100;
   int guessDigit2 = remainingGuessDigits / 10;
   int guessDigit3 = remainingGuessDigits % 10;

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

   // Check the guess
   if (guess == lottery)
   {
       System.out.println("Exact match: you win $10,000");
   }
//TODO else if statement sees if the input from user mathches the lottery number, the numbers can be in any order
   else if ()
   {
  
   }
  
   else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit1 == lotteryDigit3
           || guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3
           || guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2 || guessDigit3 == lotteryDigit3)
  
   {
       System.out.println("Match one digit: you win $1,000");
   }
  
   else
   {
       System.out.println("Sorry, no match");
   }
   input.close();
   }


}

Explanation / Answer

Hi,

Please see below the updated class.

Please comment for any queries/feedbacks.

Thanks.

PE05.java

package hmwk3;
import java.util.Scanner;
/**
* Revise the Lottery.java from self-review part, to
* generate a lottery of a three-digit number. The program
* prompts the user to enter a three-digit number and determines
* whether the user wins according to the following rules:
* If the user input matches the lottery number in the exact order, the award is $10,000.
* If all digits in the user input match all digits in the lottery number, the award is $3,000.
* If one digit in the user input matches a digit in the lottery number, the award is $1,000.
* @author DR
*
*/
public class PE05
{
public static void main(String[] args)
{
// Generate a lottery
int lottery = (int)(Math.random() * 1000);
// Prompt the user to enter a guess
Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (three digits): ");
int guess = input.nextInt();
// Get digits from lottery
int lotteryDigit1 = lottery / 100;
int remainingLotteryDigits = lottery % 100;
int lotteryDigit2 = remainingLotteryDigits / 10;
int lotteryDigit3 = remainingLotteryDigits % 10;
  
// Get digits from guess
int guessDigit1 = guess / 100;
int remainingGuessDigits = guess % 100;
int guessDigit2 = remainingGuessDigits / 10;
int guessDigit3 = remainingGuessDigits % 10;
System.out.println("The lottery number is " + lottery);
// Check the guess
if (guess == lottery)
{
System.out.println("Exact match: you win $10,000");
}
//TODO else if statement sees if the input from user mathches the lottery number, the numbers can be in any order
else if ((guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit1 == lotteryDigit3)
&& (guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3)
&& (guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2 || guessDigit3 == lotteryDigit3))
{
   System.out.println("Match one digit: you win $3,000");
}
  
else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2 || guessDigit1 == lotteryDigit3
|| guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3
|| guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2 || guessDigit3 == lotteryDigit3)
  
{
System.out.println("Match one digit: you win $1,000");
}
  
else
{
System.out.println("Sorry, no match");
}
input.close();
}

}

Sample output:

Enter your lottery pick (three digits): 486
The lottery number is 684
Match one digit: you win $3,000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote