Hey guys, i'm a bit of a new programmer taking a class.So in my code what im trying to do is to select three random numbers, and see if it matches with the numbers that the lottery throws out. The problem is where the 1st number and 3rd number selected randomly show up fine, but i have no idea on how to get the 2nd number to show up. I know my error is right here at
lotteryDigit2

    int lotteryDigit1 = lottery / 100;
        int lotteryDigit2 = lottery * 1;
        int lotteryDigit3 = lottery % 10;
        int guessDigit1 = guess / 100;
        int guessDigit2 = guess * 1;
        int guessDigit3 = guess % 10;

What equation do i do to get the 2nd number correct? Any help would be apprieciated. (I also read something about arrays, but he haven't learned those in our class yet)

Here is my complete code

import java.util.Scanner;

public class LotteryFixed {

    public static void main(String[] args) {

        int lottery = (int)(Math.random() * 1000);

        Scanner input = new Scanner(System.in);
        System.out.print("Enter your lottery pick (three digits): ");
        int guess = input.nextInt();

        int lotteryDigit1 = lottery / 100;
        int lotteryDigit2 = lottery * 1;
        int lotteryDigit3 = lottery % 10;
        int guessDigit1 = guess / 100;
        int guessDigit2 = guess * 1;
        int guessDigit3 = guess % 10;

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

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










    }

}

Edited 2 Years Ago by IchibanXD: grammar again

this code that you have works fine for generating 3 numbers, althought I really dont understand why are you using those mathematical expressions for loterryDigits

import java.util.Scanner;
public class LotteryFixed {
    public static void main(String[] args) {
        int lottery = (int)(Math.random() * 1000);
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your lottery pick (three digits): ");
        int guess = input.nextInt();
        int lotteryDigit1 = lottery / 100;
        int lotteryDigit2 = lottery * 1;
        int lotteryDigit3 = lottery % 10;
        int guessDigit1 = guess / 100;
        int guessDigit2 = guess * 1;
        int guessDigit3 = guess % 10;
        System.out.println("The lottery number is " + lottery);
    }
}

One thing you might want to take a look at is Random class and its method nextInt(), then you can get a random number in for example 0-9 to generate each of the 3 lotteryDigits separately

Also the loterry number is from 0 to 1000, so you get number for lottery 500, then in loterryDigit1 you write 500/100 =5 , then in lotteyDigit2 you write 500*1 = 500, Do you see something weird?

Edited 2 Years Ago by Slavi

Well the reason i'm doing mathematical expressions is that the book gave me the program but i was suppose to revise it so it works for a three digit number (The original was for a 2 digit number). The reason i posted this was to find out how i would get lotteryDigit2 to work correctly

I solved it! I did this and i worked fine

int lotteryDigit1 = lottery / 100;
        int lotteryDigit2 = ((lottery % 100) - (lottery % 10)) /10;
        int lotteryDigit3 = lottery % 10;

        int guessDigit1 = guess / 100;
        int guessDigit2 = ((guess % 100) - (guess % 10)) /10;
        int guessDigit3 = guess % 10;
This question has already been answered. Start a new discussion instead.