Reputation: 3
hello guys i am really stuck, its about a lottery program that randomly generate a four-digit number and prompt the user to enter a four-digit number without using array and following this rules -if the user input match the lottery number in exact order the award is $10000 -if the user input match the four-digit in different order the award is $5000 -if the user input match the three-digit number in different order the award is $2000 -if any 1 or 2-digit in the user input match the lottery the award is $500
i did it for a 3-digit number but i don't know how to do it for a four-digit number without arrays.this is what i have done :
import java.util.Scanner;
public class Programming {
public static void main(String[] args) {
// Generate a lottery
int lottery = (int) (Math.random() * 1000);
// 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 " + lotteryDigit1
+ lotteryDigit2 + lotteryDigit3);
// Check the guess
if (guess == lottery) {
System.out.println("Exact match: you win $10,000");
} else if ((guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit3)
|| (guessDigit1 == lotteryDigit2
&& guessDigit1 == lotteryDigit3 && guessDigit3 == lotteryDigit1)
|| (guessDigit1 == lotteryDigit3
&& guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit2)
|| (guessDigit1 == lotteryDigit3
&& guessDigit2 == lotteryDigit2 && guessDigit3 == lotteryDigit1)
|| (guessDigit1 == lotteryDigit1
&& guessDigit2 == lotteryDigit3 && guessDigit3 == lotteryDigit2)) {
System.out.println("Match all digits: you win $5,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");
}
}
}
thank you for your help
Upvotes: 0
Views: 1884
Reputation: 2516
Sorry for taking a while to get back to you. Been a busy couple days. As requested, here is the complete program. Note that this isn't entirely your original program. I removed a couple things and added a few more in. Perhaps most importantly, I changed the function that I gave you previously. I originally answered your question to try to only give you the four digit match but if I am writing out the whole thing it makes more sense to generalize a bit. Because your lottery awards are based on the number of matching digits, it makes a lot of sense to make a function that counts how many digits match. This simplifies your if-else code a whole lot.
import java.util.Scanner;
public class Programming {
/*
* get the number of matching digits between the guess and the
* answer, ignoring repeated matches
*/
public static int numberDigitsMatch(String guess, String answer) {
int numberMatch = 0;
int currentIndex = 0;
int matchingIndex;
while (currentIndex < guess.length()) {
// check if the current digit of the guess is in the answer
matchingIndex = answer.indexOf(guess.charAt(currentIndex));
if (matchingIndex < 0) {
currentIndex++;
}
else {
currentIndex++;
numberMatch++;
// remove the no longer relevant character from the answer
answer = answer.substring(0, matchingIndex) +
answer.substring(matchingIndex + 1);
}
}
return numberMatch;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// generate the winning number
int lotteryNumber = (int) (Math.random() * 10000);
String lotteryString = "" + lotteryNumber;
System.out.println("The lottery number is " + lotteryString);
// Prompt the user to enter a guess
System.out.print("Enter your lottery pick (four digits): ");
// get the user's guess
int guessNumber = in.nextInt();
String guessString = "" + guessNumber;
// NOTE: these guessDigit numbers are not necessary. I am leaving
// them here so you can see how to pick out individual digits
int guessDigit1 = guessNumber % 10;
int guessDigit2 = (guessNumber / 10) % 10;
int guessDigit3 = (guessNumber / 100) % 10;
int guessDigit4 = (guessNumber / 1000) % 10;
int lotteryDigit1 = lotteryNumber % 10;
int lotteryDigit2 = (lotteryNumber / 10) % 10;
int lotteryDigit3 = (lotteryNumber / 100) % 10;
int lotteryDigit4 = (lotteryNumber / 1000) % 10;
System.out.println("The lottery number is " + lotteryString);
int numMatchingDigits = numberDigitsMatch(guessString, lotteryString);
if (guessNumber == lotteryNumber) {
System.out.println("Exact match: you win $10,000");
}
else if (4 == numMatchingDigits) {
System.out.println("Match all digits: you win $5,000");
}
else if (3 == numMatchingDigits) {
System.out.println("Match three digits: you win $2,000");
}
else if (2 == numMatchingDigits) {
System.out.println("Match two digits: you win $500");
}
else if (1 == numMatchingDigits) {
System.out.println("Match two digit: you win $500");
}
else {
System.out.println("Sorry, no match");
}
}
}
Looks like I did not understand correctly. I believe there are multiple ways to get a given digit of a number but I think this is the easiest to think about. You divide by 1000 to shift the thousands digit into the 1's place then use the modulus operator to get rid of anything that isn't in the 1's place. This leaves you with nothing but the fourth digit of the number.
public static int getFourthDigit(int num) {
return (num / 1000) % 10;
}
If I understand correctly, your difficulty is with matching different ordered four digit numbers. The most obvious way to do this is to just check all possible orderings of the four digits; there are only 15 where they are not exactly equal. However, typing out 15 conditions is prone to errors and just plain boring. It would be twice as boring and tedious if you instead needed to do this with five digits.
Here is a function that avoids this by using String
instead of int
. It repeatedly checks the first character of the guess and checks if the character is in the answer. It then removes that matching character from the answer to avoid a case like 1111
and 1112
, where every character in the guess is also in the answer but they do not match.
public static boolean digitsMatch(String guess, String answer) {
int matchingIndex;
while (guess.length() > 0) {
// check if the first digit of the guess is in the answer
matchingIndex = answer.indexOf(guess.charAt(0));
// if not, there cannot possibly be four matches
if (matchingIndex < 0) {
return false;
}
// look at the rest of the guess
guess = guess.substring(1);
// and remove the no longer relevant character from the answer
answer = answer.substring(0, matchingIndex) +
answer.substring(matchingIndex + 1);
}
return true;
}
Upvotes: 1