java - GUI guessing game- how to program the guesses correctly -
i new website. below have put code. little background on gui--- guessing game. computer has thought of random number between 1 , 100 , user must try guess it. when user inputs number, program must tell user if number computer thinking of lower or higher or equal number user guessed. user has 8 tries guess number. if user fails guess number within 8 tries, program show user number , automatically shut down. question is- can tell me why guessing part of game not working? i.e. when enter 100 number want guess, program says "guess higher" , plus, program not correctly tell higher, lower, or equal. thanks!
import java.awt.dimension; import java.util.arrays; import java.util.list; import java.util.random; import java.util.scanner; import java.awt.font; import java.awt.layoutmanager; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class board extends jpanel{ private int guesscount; private jlabel jlabel1 = new jlabel("guessing game"); private jlabel jlabel2 = new jlabel("the computer think of number between"); private jlabel jlabel3 = new jlabel(" 1 , 100 , try guess it."); private jlabel jlabel5 = new jlabel("if not number within 8 guesses,"); private jlabel jlabel6 = new jlabel("the program show number , will"); private jlabel jlabel7 = new jlabel("automatically shut down."); private jlabel jlabel4 = new jlabel("enter guess: "); private jtextfield guessinput = new jtextfield(); private jbutton submit = new jbutton(); board() { this.setlayout(new borderlayout()); jpanel subpaneltitle = new jpanel(); jlabel1.setbackground(color.white); jlabel1.setforeground(color.green); font font1 = new font("monospaced", font.bold, 50); jlabel1.setfont(font1); jlabel2.setbackground(color.white); jlabel2.setforeground(color.magenta); font font2 = new font("monospaced", font.bold, 18); jlabel2.setfont(font2); jlabel3.setbackground(color.white); jlabel3.setforeground(color.magenta); font font3 = new font("monospaced", font.bold, 18); jlabel3.setfont(font3); jlabel4.setbackground(color.white); jlabel4.setforeground(color.blue); font font4 = new font("monospaced", font.bold, 18); jlabel4.setfont(font4); jlabel5.setbackground(color.white); jlabel5.setforeground(color.black); font font5 = new font("monospaced", font.bold, 16); jlabel5.setfont(font5); jlabel6.setbackground(color.white); jlabel6.setforeground(color.black); jlabel6.setfont(font5); jlabel7.setbackground(color.white); jlabel7.setforeground(color.black); jlabel7.setfont(font5); guessinput.setpreferredsize(new dimension(50,50)); submit.setpreferredsize(new dimension(100,20)); submit.setforeground(color.red); submit.settext("submit"); subpaneltitle.add(jlabel1); subpaneltitle.add(jlabel2); subpaneltitle.add(jlabel3); subpaneltitle.add(jlabel5); subpaneltitle.add(jlabel6); subpaneltitle.add(jlabel7); subpaneltitle.add(jlabel4); subpaneltitle.add(guessinput); subpaneltitle.add(submit); subpaneltitle.setpreferredsize(new dimension(600, 350)); guesscount = -1; jpanel panel = new jpanel(); panel.setlayout(new gridlayout(10, 10)); this.add(subpaneltitle,borderlayout.east); submit.addactionlistener(new buttonlistener()); } private class buttonlistener implements actionlistener { public void actionperformed(actionevent e) { string actioncommand = e.getactioncommand(); scanner sc=new scanner(system.in); { random generator = new random(); int max = 100; int min = 1; int random = generator.nextint((max-min+1)-min); int getguessinput = 0; guesscount++; if(guesscount > 7){ joptionpane.showmessagedialog(null, "you out of guesses. lose. number " + random); system.exit(0); } if(getguessinput < random){ joptionpane.showmessagedialog(null, ("guess higher")); } else if(getguessinput > random){ joptionpane.showmessagedialog(null, ("guess lower")); } else{ joptionpane.showmessagedialog(null, ("congratulations, win! " + " took " + guesscount + " guesses ")); } } } } }
int getguessinput = 0;
is never changed. you're dealing guess of 0, , guesscount
gets reset well.
you should make these ints accessible outside actionlistener, , make sure getguessinput set.
the gory details of implementation left exercise reader.
Comments
Post a Comment