Hey guys. Before I describe my problem, I'd just like to say that I'm completely new to programming, and I'm currently in the first year of my program.

I have an assignment to create a "Lottery winning program". The program generates a random seven digit number, and matches it to the input from the user (which is also seven digits).
We are supposed to use the Math.random method to generate our numbers. After comparing the randomly generated number and user input, the user wins a prize based on certain criteria in the numbers you matched. Example:

- If all the digits are matched, the user will get a bumper prize ( $1000).
- If all the even place number matches, the prize money will be $ 500. Eg If 1239557 is the Winning number, 3249155 is a number in which all even place numbers match.
- If all the odd place numbers match, the prize money will be $250
- If first and the last digits match, the prize money will be $100

From what I understand, in order to do this, I must initialize 7 new variables. In a similar exercise where you only have to generate a number with 2 digits, I was did the following:

public class lotteryTest{


	public static void main(String[] args){
		
		// Generate a lottery
		
		int lottery = (int)(Math.random() * 100);
	    
		// Promt the user to enter a guess
		
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter your lottery number (use 2 digits): ");
		
		int guess = input.nextInt();
		
		// Get digits from the lottery
		
		int lotteryDigit1 = lottery / 10;
		int lotteryDigit2 = lottery % 10;
		
		// Get digits from the guess
		
		int guessDigit1 = guess / 10;
		int guessDigit2 = guess % 10;
		
		System.out.println("The lottery number is " + lottery);
		
		// Check the guess
		
		if (guess == lottery)
			System.out.println("Exact match! You win $10,000");
		else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2)
			System.out.println("You matched both numbers. You win $3,000");
		else if (guessDigit1 == lotteryDigit1
				|| guessDigit1 == lotteryDigit2
				|| guessDigit2 == lotteryDigit1
				|| guessDigit2 == lotteryDigit2)
			System.out.println("You matched one digit! You win $1,000");
		else
			System.out.println("Sorry, you did not get any matches!");
			
		
		
		
		

	}
	
}

This seems to work perfectly, because "guess % 10" obtains the last digit from guess, and guess / 10 obtains the first digit. Now, onto my problem:

When I have to generate seven digits instead of 2, I have to do something like this:

int guessDigit1 = guess / 1000000;
		int guessDigit2 = ???;
		int guessDigit3 = ???;
		int guessDigit4 = ???;
                int guessDigit5 = ???;
                int guessDigit6 = ???;
                int guessDigit7 = ???;

My problem is that asides from obtaining guessDigit1, I have no idea how to get the value for guessDigit2, guessDigit 3, and so on.

For my previous example with two digits, the text book did not really explain how "guess / 10" obtains the first digit and how "guess % 10" obtains the last digit. All I know is this method will not work when I have to use more than two digits.

I'm having a lot of trouble writing this program, and the due date is approaching. If anyone could provide any help or even point me in the right direction with what I'm supposed to do, it would be greatly appreciated. Again, I apologize if I didn't explain things well enough, as I'm completely new to this.

Thank you for your time.

Recommended Answers

All 5 Replies

Have you guys done arrays yet?
Easiest way is this.


Sorry I misread the question.

Change the random generator to generate random numbers from 0-9.
Generate random number 7 times. Each time you generate a number, store it in separate arrays (7 elements). One number for each array.

Now, I'm a little unsure how you are suppose to take input. Are you suppose to ask 7 times to get 7digits numbers? or enter all 7digits at once and read each of those numbers then compare it?

Either way, once you've decided how you are going to take input, store each input into a separate array (7 elements).

Then compare the two arrays. For example. ArrayRandom[0] = 3 VS ArrayInput[0] = 3. So if ArrayRandom[1] and ArrayInput[1] match then here's what you win. Etc...

No, we haven't done arrays yet.

The program generates ONE seven digit number. (Ex: 1234567)

Then, it prompts the user to also enter ONE seven digit number. Ex: (2345678)

The way the user inputs the number can be done with either Scanner:

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

or .showInputDialog

guess = Integer.parseInt(JOptionPane.showInputDialog( null, "Please enter your lottery number)", " Lottery number", JOptionPane.INFORMATION_MESSAGE));

Then the program compares the user input and the randomly generated number (both 7 digits). From there on, I will use if/switch statements to do the following:

- If all the digits are matched, the user will get a bumper prize ( $1000).
- If all the even place number matches, the prize money will be $ 500. Eg If 1239557 is the Winning number, 3249155 is a number in which all even place numbers match.
- If all the odd place numbers match, the prize money will be $250
- If first and the last digits match, the prize money will be $100


The problem is separating that one seven digit number into seven different variables. I know to obtain the first digit, you can divide the value by 1000000

guessDigit1 = guess / 1000000;

But for the other six, I'm completely lost.

The way you did it, using the operators "/" rounds down w/e value you put. Eg. 1/2 = 0
The "%" operator only takes the decimal value and either rounds up or down.

You can try doing it like this.

int lottery;
     Scanner input = new Scanner(System.in);
     int guess;
 
     boolean evenCheck =true;
     boolean oddCheck = true;
     
     /* Each iteration, verify whether the guess matches the generated number
      Prize Condition: if same number at even. this is where i = 2,4,6
                        if same number at odd. this is where i = 1,5,7
                        if your check for the even and odd matches, then everything matches.
                    */
     for (int i = 1; i<=7; i++){
         lottery = (int)(Math.random() * 10);
		System.out.print("Enter your lottery number: ");
		guess = input.nextInt();

                //do you if statements here
                //Check for even if
                if (i == 2 || i == 4 || i == 6){
                    if (lottery == guess){
                         //if it matches just exit the if statement
                    }
                    else{
                         //if at some point the lottery and guess dont match set evencheck to false
                        evenCheck = false;
                    }
                    
                }
                //check for odd
                if (i == 1 || i == 5 || i == 7){
                    if (lottery == guess){
                        
                    }
                    else{
                        oddCheck = false; //if at some point the lottery and guess dont match
                    }
                }


                if(oddCheck == true && evenCheck == true){
                    //Every digit matches. Display the prize
                }

// expand on this code to see if all the even sections match and the same for odd.

Have you learned substrings yet??

If yes, then I think this may help.

1. Computer generates a seven-digit number.
2. Get 7-digit Input from user (Store this as String length 7).

3. You need to clarify the rules. If all numbers match, he wins the bumper prize but since they match will the user win the even, odds, and 1st-last digit prize??

4. If the rules are clarified, then you need to compare the numbers.

- 1st, compare if all digits are the same,. You can do this by

//comp generated number shpuld be in String format
CompGeneratedNumber.equals(UserInput)

if that is true then user wins the bumper prize then exit.

- Next, if not then compare for even and odds using substring. You can do this using a for Loop.

for (int i = 0; i < 7; i = i + 2) {
          //compare using substring
      if (compGeneratedNumber.substring(i, i+ 1).equals(UserInput.substring(i, i +1)
          // if not true then you need to exit this loop
      
          // this for loop is for even... for odd just change start from position 1.

- Lastly, you can also use substrings to compare the 1st and last digits.

Just got it working :)

Thank you all for helping, it's greatly appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.