java - Why does my program prompt for input over and over? -
you write java programme play game of pico, fermi, bagel. here rules of game:
the computer generate "secret" 3 digit number @ random. first number not 0, , digits different. user tries guess number. if user guesses correctly, game over.
if not, computer gives hint , player tries again. hints:
for each digit matches secret number in proper place, computer prints "fermi"
for each digit matches, not in proper place, computer prints "pico"
if none of digits match, computer prints "bagels"
the programme have main class , bagels class. bagels class phone call 3 other methods to
1) generate secret number
2) determine whether current guess winner
3) evaluate current guess , print hints
my issue: when run program, asks me come in 3 digit number, repeatedly asks me come in 3 digits on , over. i'm pretty sure problem has methods in bagels class. compiler saying generatesecretnumber
, printhint
methods unused. thing i'm not sure how go making used.
main class
package assignment.iii; import javax.swing.joptionpane; import java.util.scanner; public class assignmentiii { public static void main(string[] args) { int playagain = joptionpane.showconfirmdialog(null, "would play?", "message", joptionpane.yes_no_option); while (playagain == joptionpane.yes_option) { bagels mybagels = new bagels(); mybagels.playgame(); mybagels.randnumber = 0; playagain = joptionpane.showconfirmdialog(null, "would play again?", "message", joptionpane.yes_no_option); } } }
bagels class
package assignment.iii; import java.util.random; import javax.swing.joptionpane; public class bagels { public int randnumber; private int guess; private int rand1, rand2, rand3; private int guess1, guess2, guess3; private int guesscount; public void playgame() { if (guess1 == 0 || guess1 == guess2 || guess2 == guess3 || guess1 == guess3); joptionpane.showmessagedialog(null, "please come in number" + "the first digit can't 0 , no digits can repeat"); { guess = integer.parseint(joptionpane.showinputdialog("enter 3 digit number")); } while (guess != randnumber); if (guess == randnumber) system.out.println("it took " + guesscount + " guesses."); } private int generatesecretnumber() { random randn = new random(); homecoming randn.nextint(999)+1; } private void printhint(string guess) { if (randnumber == guess) system.out.println("correct"); else { guess1 = (guess) / 100; guess2 = (guess % 100) / 10; guess3 = (guess % 100) % 10; } if (guess1 == rand1) { system.out.println("fermi"); } if (guess2 == rand2) { system.out.println("fermi"); } if (guess3 == rand3) { system.out.println("fermi"); } if (guess2 == rand1) { system.out.println("pico"); } if (guess3 == rand1) { system.out.println("pico"); } if (guess1 == rand2) { system.out.println("pico"); } if (guess3 == rand2) { system.out.println("pico"); } if (guess1 == rand3) { system.out.println("pico"); } if (guess2 == rand3) { system.out.println("pico"); } else if(guess1 != rand1 && guess1 != rand2 && guess1 != rand3 && guess2 != rand1 && guess2 != rand2 && guess2 != rand3 && guess3 != rand1 && guess3 != rand2 && guess3 != rand3) { system.out.println("bagels"); } guesscount++; } }
generatesecretnumber , printhint methods unused.
yep, that's core issue.
you initialize number guessed zero
mybagels.randnumber = 0;
and never set value, loop
while (guess != randnumber);
will go on until guesses 0.
only thing i'm not sure how go making used.
there many options. 1 alternative is, @ origin of playgame(), invoke it
randnumber = generatesecretnumber();
java
No comments:
Post a Comment