Greetings. I'm new here and I apologize in advance if I'm asking in the wrong area. Please move me if I'm wrong.
I am in an intro to java programming class and we have a bonus homework question. I missed the first homework because I couldn't access the homework. Each HW is worth 10 points and this bonus question is worth 3. The question is to generate a 3 digit lottery, use scanner to ask for a 3 digit guess, then the user wins $10,000 if all the numbers match in the correct order, they win $3,000 if all numbers match in any order, and they win $1,000 if any one number matches.
I can't tell if the 3 in any order are right because I haven't guessed 3 correct numbers. I know the any one matching number is wrong, however... because it says, "Sorry, you win nothing" whenever I guess one or more right. (Edited...I was entering the numbers with spaces between them, as if they were individual scanner inputs. When I just enter a 3 digit interger, it works. Please tell me if my logic looks right for the 3 in any order) I've been staring at this code since Friday and any help you can provide would be appreciated more than you know. We haven't covered this yet in class....that's why it's a bonus question. Here's what I have:
import java.util.Scanner; //Scanner is in the java.util package public class ThreeDigitLotteryHW { public static void main(String[] args){ //create 3 digit random forced interger int lottery = (int)(Math.random() * 1000); Scanner input = new Scanner(System.in); //Prompt the user to enter 3 intergers System.out.println("Please enter your lottery pick(3 Digits): "); int guess = input.nextInt(); //Turn guess interger into 3 individual intergers int guessDigit1 = guess / 100; int guessDigit2 = (guess / 10) % 10; int guessDigit3 = guess % 10; //Turn random 3 digit interger into 3 individual intergers int lotteryDigit1 = lottery / 100; int lotteryDigit2 = (lottery / 10) % 10; int lotteryDigit3 = lottery % 10; //Print converted random intergers System.out.println("The lottery number is: " + lotteryDigit1 + " " + lotteryDigit2 + " " +lotteryDigit3); //If user guesses all numbers in the correct order, they win $10,000 if (guess == lottery) System.out.println("You win $10,000."); //If 1st digit is correct and last two are correct in any order, user wins $3,000 else if ((guessDigit1 == lotteryDigit1) && (guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3) && guessDigit3 == lotteryDigit2 || guessDigit3 == lotteryDigit3) System.out.println("You win $3,000."); //If 2nd digit is correct and 1st and 3rd are correct in any order, user wins $3,000 else if ((guessDigit2 == lotteryDigit2) && (guessDigit3 == lotteryDigit3 || guessDigit3 == lotteryDigit1) && guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit3) System.out.println("You win $3,000."); //If 3rd digit is correct and 1st and 2nd are correct in any order, user wins $3,000 else if ((guessDigit3 == lotteryDigit3) && (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2) && guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit1) System.out.println("You win $3,000."); //If 1st guess is correct in any lottery result, user wins $1,000 else if (guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit2|| guessDigit1 == lotteryDigit3) System.out.println("You win $1,000"); //If 2nd guess is correct in any lottery result, user wins $1,000 else if (guessDigit2 == lotteryDigit1 || guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit3) System.out.println("You win $1,000"); //If 3rd guess is correct in any lottery reult, user wins $1,000 else if (guessDigit3 == lotteryDigit1 || guessDigit3 == lotteryDigit2 || guessDigit3 == lotteryDigit3) System.out.println("You win $1,000."); /*As we have used all other results, if no lottery results * are guessed correctly, print, "Sorry, you win nothing" */ else System.out.println("Sorry, You win nothing."); //Close the input input.close();