Reputation: 7
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:
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 at Rule 3 I am not understanding why it is not recognizing a winning digit. For example, My guess numbers will be 142 and the lottery numbers will be 554 and it will say "No Match, Better Luck Next Time" rather than "Matched one number even though there is one correct number in my guess.
import java.util.Scanner;
public class Lottery {
public static void main(String[] args) {
//generate random lottery numbers
int lottery = (int)(Math.random() * 1000);
//ask user to enter a 3 digit integer and store those numbers
Scanner numbers = new Scanner(System.in);
System.out.println("Please enter your lottery pick. Three Digits Only");
int guess = numbers.nextInt();
// numbers for lottery
int lotteryDigit1 = lottery % 100;
int lotteryDigit2 = lottery % 100 / 10;
int lotteryDigit3 = lottery / 100;
//get digits from guess
int guessDigit1 = guess % 100;
int guessDigit2 = guess % 100 / 10;
int guessDigit3 = guess / 100;
//print out lottery numbers
System.out.println("The lottery numbers are: " + lottery);
if (guess == lottery)
System.out.println("Exact Match!!! 10,000$ Prize!!!");
else if (guessDigit2 == lotteryDigit1
&& guessDigit1 == lotteryDigit2
&& guessDigit3 == lotteryDigit3)
System.out.println("Matched all numbers!!! 3,000$ Prize!!!");
//Rule 3
else if (guessDigit1 == lotteryDigit1
|| guessDigit1 == lotteryDigit2
|| guessDigit1 == lotteryDigit3
|| guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2
|| guessDigit2 == lotteryDigit3
|| guessDigit3 == lotteryDigit1
|| guessDigit3 == lotteryDigit2
|| guessDigit3 == lotteryDigit3)
System.out.println("Mathed one number!!! 1,000$ Prize!!!");
else enter code here
System.out.println("No Match, Better Luck Next Time!");
Upvotes: 0
Views: 5121
Reputation: 508
Well, here's a similar solution in Javascript
I came up with:
console.log('Program started...');
// Settings
const persist = true; // 1. Make sure the user inputs a number only, loop until the user inputs a number
// Function: Generates 3 random numbers, between 1 and 10 (inclusive)
const generateNumbers = () => {
var arr = [];
while (arr.length < 3) {
var r = Math.floor(Math.random() * 10) + 1;
arr.push(r);
}
return arr;
};
const winningNumbers = generateNumbers(); // An array of the 3 winning numbers
// Function: Prompt user for a number
const getUserNumber = (type = '', err = '') => {
var q = "";
if (err.trim() != '') q = "\n" + err + "\n";
q += "Enter your ";
q += type;
q += " number";
var inp = prompt(q);
if (persist && inp != null) {
if (inp.trim() == '' || isNaN(inp.trim())) getUserNumber(type, "Error: Invalid input!");
}
return inp;
};
var userNumbers = []; // An array of the 3 user selected numbers
// Loop user input
['first', 'second', 'third'].forEach((e, i) => {
let n = getUserNumber(e);
userNumbers.push(Number(n));
});
var correct = 0; // Store correct matches
var _winningNumbers = [...winningNumbers]; // Store winning numbers temporarily
// Process correct answers
userNumbers.forEach(n => {
let count = _winningNumbers.filter(num => {
return num == n
}).length;
if (count >= 1) {
correct += 1;
_winningNumbers.splice(_winningNumbers.indexOf(n), 1);
}
});
// Process user winnings
const getWinnings = () => {
var winnings = "";
switch (correct) {
case 1:
winnings = "$100";
break;
case 2:
winnings = "$200";
break;
case 3:
winnings = "$500";
break;
default:
winnings = "$0";
break;
}
return winnings;
}
// Alert winnings
var resp = "";
resp += "The computer's numbers are ";
resp += winningNumbers.join(" ");
resp += "\n";
resp += "Your guesses were ";
resp += userNumbers.join(" ");
resp += "\n";
resp += "You got ";
resp += correct;
resp += " right";
resp += "\n";
resp += "You've won ";
resp += getWinnings();
// Function: Show response to user and prompt 'Play again?''
const showResponse = (err = '') => {
var q = resp;
q += "\n\n";
if (err.trim() != '') q += "\n" + err + "\n";
q += "Play Again? Y or N";
var inp = prompt(q);
if (persist && inp != null) {
if (inp.trim() == '' || !isNaN(inp.trim())) showResponse("Error: Invalid input!");
}
if (inp.toLowerCase() == 'y') window.location.reload();
};
showResponse();
console.log('Winning numbers: ', winningNumbers);
console.log('Selected numbers: ', userNumbers);
console.log('Correct numbers: ', correct);
console.log('Amount won: ', getWinnings());
console.log('...program completed!');
Here's the link to the repo.
Upvotes: 0
Reputation: 11
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //make new scanner
// Generate a random three-digit number
// Prompt the user to enter a three-digit number
//Generate random number with 3 diggits
int randomLottery = 100 + (int)(Math.random() * ((999 - 100)+1));
//Start scanner
//Prompt user to enter three digits
System.out.println(" Enter the Guess number (three digit) integer");
int GuessUser = input.nextInt();
int GUESS1 = GuessUser /100;
int remainGuess = GuessUser % 100;
int guess2 = remainGuess /10;
int guess3= remainGuess % 10;
//Get 3 digits from lottery
int d1 = randomLottery /100;
int remainL = randomLottery % 100;
int d2 = remainL /10;
int d3= remainL % 10;
System.out.println(" The lottery numbers are " + d1 + " " + d2 + " " + d3 + "!");
//**Check the guess and print out the award system.
/* if users input matches the lottery input in exact order price is 10000$*/
if (GuessUser == randomLottery ){
System.out.println("You have won $10000. HAMBALYO!!!!");
/* if all the DIGITS EQUAL $3000 */
}else if (((GUESS1 == d1) || (GUESS1 == d2) || (GUESS1 == d3)) &&
(( guess2 == d1) || (guess2 == d2) || (guess2 == d3)) &&
((guess3 == d1) || (guess3 == d2) || (guess3 == d3))){
System.out.println("ALL DIGITS MATCH! You have won $3000.");
/* if at least one digit matches the lottery number the award is 1000$ */
}else if (((GUESS1 == d1) || (GUESS1 == d2) || (GUESS1 == d3)) ||
((guess2 == d1) || (guess2 == d2) || (guess2 == d3)) ||
((guess3 == d1) || (guess3 == d2) || (guess3 == d3))){
System.out.println("At least one number is correct. You win $1000 ");
}else
System.out.println ("No match");
}
}
Upvotes: 1
Reputation: 61
So, in this case there's a couple very small bugs. When you're attempting to get digit 1, you modulo by 100. I believe you're meaning to modulo by 10 to get the digit in the 1s place. You can view this by putting in a debugger if you know how or simply printing out the values you have temporarily while you work out the bugs.
One thing you didn't mention, but I think you'll see later is that your code for determining if the 3 numbers match is slightly incorrect:
else if (guessDigit2 == lotteryDigit1
&& guessDigit1 == lotteryDigit2
&& guessDigit3 == lotteryDigit3)
doesn't compare the correct pairs of digits.
Upvotes: 0
Reputation: 8318
The problem is this piece of code -
int lotteryDigit1 = lottery % 100;
For lottery
= 554, lotteryDigit1
will be set to 54. Similarly, guessDigit1
is set to 42. Instead, lottery % 10 should give you the least significant digit here.
Note - It's probably easy to figure out this flaw easily in this case, but learning how to use a debugger will help you in similar issues in future.
Upvotes: 1