Search code examples
javauser-input

Creating a Lottery


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: (RULE 1). If the user input matches the lottery number in the exact order, the award is $10,000. (RULE 2). If all digits in the user input match all digits in the lottery number, the award is $3,000. (RULE 3). If one digit in the user input matches a digit in the lottery number, the award is $1,000.

I need help getting my program to function as intended with all that is seen in the code. No arrays, no strings, nothing other than what is already there. My issue is that I cannot get a guess of 110 to ignore rule 2. I set it to 100 to test all the rules.

import java.util.Scanner;

public class NewClass {
    public static void main(String[] args) {
        int lottery = 100;

 // 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 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);

 // RULE1 Check the guess
        if (guess == lottery)
            System.out.println("Exact match: you win $10,000");
 // RULE2   
        else if ((guessDigit1 == lotteryDigit1
            || guessDigit1 == lotteryDigit2 
            || guessDigit1 == lotteryDigit3)
            && (guessDigit2 == lotteryDigit1
            || guessDigit2 == lotteryDigit2
            || guessDigit2 == lotteryDigit3)
            && (guessDigit3 == lotteryDigit1
            || guessDigit3 == lotteryDigit2
            || guessDigit3 == lotteryDigit3))
            System.out.println("Match all digits: you win $3,000");
    // RULE3
        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");
    }
 }

UPDATE:

import java.util.Scanner;
    public class NewClass {
        public static void main(String[] args) {
        int lottery = 456;

 // 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 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;

 // RULE1 Check the guess
        if (guess == lottery)
            System.out.println("Exact match: you win $10,000");
 // RULE2   
        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("Match all digits: you win $3,000");
// RULE3
        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");
    }
 }

This seems to work above all else. I've been cycling through numbers to test against the guess. I've not encountered a wrong guess with this. && guessSum == lotterySum) used only with RULE2.


Solution

  • I think that the problem is that you're not accounting for repeat digits. You could check to make sure all the digits add up to the sample number, ie:

    1+1+0 = 2
    1+0+0 = 1
    1!=2
    Move onto rule 3. 
    

    Using this code:

    guessDigit1  + guessDigit2 + guessDigit3 == lotteryDigit1 + lotteryDigit2 + lotteryDigit3
    

    I don't know if this will solve all of your problems, but it's a start, and it should solve your immediate issue with the '110' value. Good luck!