java - How to check if three numbers are guessed? -
hi on community. i'm java newbie. no homework, i'm passionate programming , want learn more. stuck 1 exercise.
basically have create simple lottery game. user has input 1 number 0 999, 3 digit number.
- if guess same, prize 10.000 $,
- if guess same (digits guessed not in order) prize 3,000 $
- if guess not same (digits guessed == 1) prize 1,000 $.
here's code far: don't know how deal condition 2 , 3. have hint or comment?
import java.util.scanner; public class test { public static void main(string[] args) { scanner input = new scanner(system.in); system.out.println("please enter 3 digit number"); int guess = input.nextint(); //generate number: int lottery = (int)(math.random() * 1000); if ((guess > 999) || (guess < 100)) { system.out.println("the number not 3 digit number. system exit now."); system.exit(1); } else { // extract digits lottery number int lotterydigit1 = lottery / 100; int lotterydigits = lottery % 100; int lotterydigit2 = lottery / 10; int lotterydigit3 = lottery % 10; // extract digits guessed number int guessdigit1 = guess / 100; int remainingdigits = guess % 100; int guessdigit2 = guess / 10; int guessdigit3 = guess % 10; system.out.println("the lottery number is: " + lottery); // check guess: if (guess == lottery) { system.out.println("exactly number was."); } else if { // digits guessed, not in exact order (eg. 332 = 233) } else if { // 1 digit guessed (eg. 332 = 442) } } } }
could have comment on code? readable or horribly written? don't have clue since new programming. thanks.
with array, can count how many digits real number , guessed number contains, , perform comparisons that. first make 2 arrays of length 10 (one slot each digit), increase each relevant "slot" 1 every digit in real number. same guess, , compare.
i'm recommending approach because scale guesses 4, 5, or 15 digits, whereas coding these checks hand turn problem lots of duplicate code.
if, reason, you're not allowed use arrays, manually counting - sounds tempting check if each digit exists in other set of digits, you'll run trouble duplicate digits (consider 112, which, when compared 122, contain "all" digits; not in right amounts!). lead lot of duplicate code.
it'd this...
int matchingdigits = 0; if ( guessdigit1 == lotterydigit1 || guessdigit1 == lotterydigit2 || guessdigit1 == lotterydigit3) { matchingdigits++; } if ( guessdigit2 == lotterydigit1 || guessdigit2 == lotterydigit2 || guessdigit2 == lotterydigit3) { matchingdigits++; } if ( guessdigit3 == lotterydigit1 || guessdigit3 == lotterydigit2 || guessdigit3 == lotterydigit3) { matchingdigits++; }
as can see, it's lot of duplicate code, , doesn't handle 112 vs 122 case. handle that, you'd have split each check apart , cross out numbers you've "used"... possible, leads lengthy code.
here's how you'd it...
boolean digit1used = false; boolean digit2used = false; boolean digit3used = false; int matchingdigits = 0; if ( guessdigit1 == lotterydigit1) { matchingdigits++; digit1used = true; } else if ( guessdigit1 == lotterydigit2) { matchingdigits++; digit2used = true; } else if ( guessdigit1 == lotterydigit3) { matchingdigits++; digit3used = true; } if ( guessdigit2 == lotterydigit1 && !digit1used) { matchingdigits++; digit1used = true; } else if ( guessdigit2 == lotterydigit2 && !digit2used) { matchingdigits++; digit2used = true; } else if ( guessdigit2 == lotterydigit3 && !digit3used) { matchingdigits++; digit3used = true; } if ( guessdigit3 == lotterydigit1 && !digit1used) { matchingdigits++; } else if ( guessdigit3 == lotterydigit2 && !digit2used) { matchingdigits++; } else if ( guessdigit3 == lotterydigit3 && !digit3used) { matchingdigits++; }
note left out checks digitused
in first checks , sets digitused
third set of checks - because we're not going use them anymore. can see, it's long , it's faking arrays. hence why it's not recommended so.
imagine solution 15 digit array! it'd (n + 1 + (n*3*n) + n + n) lines long - 15 digitused
declarations, 1 matching digits, 15 digit comparisons of 3 lines 15 digits, plus 15 closing braces , 15 blank lines in between - total of 721 lines! that's why should use arrays this, , not checking hand.
if you've learned methods already, suggest put digit matcher in separate function; way can keep digit matching logic , result-to-prize matching logic separated.
Comments
Post a Comment