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.