Computer Science Canada hey need help wit code |
Author: | AHHNOOB [ Wed Apr 21, 2004 7:36 pm ] |
Post subject: | hey need help wit code |
hey i got a problem....this program basically checks to see if ur close to the number...it will output if ur close or not...and show a pic..when i run it the program....it highlights "static" and i dont knw wats wrong.....and also i think when i run it and click the button it will always get a new number...how do i fix that? import java.applet.*; import java.awt.*; import java.awt.event.*; public class Guess extends Applet implements ActionListener { TextField txtguess; Label lblguess, lblenter, lbltitle; Image arrow1, arrow2, smile; Button btnguess; int guesspic, ANSWER, guess; public void init () { arrow1 = getImage (getCodeBase (),"arrow1.jpg"); arrow2 = getImage (getCodeBase (),"arrow2.jpg"); smile = getImage (getCodeBase (), "smile.jpg"); lbltitle = new Label ("Guess a number between 1and 100"); lblenter = new Label ("Enter your guess"); btnguess = new Button ("High or Lower"); add (btnguess); add (lbltitle); add (lblenter); add (lblguess); add (txtguess); btnguess.setActionCommand ("High or Lower"); btnguess.addActionListener (this); } public void actionPerformed (ActionEvent e) { String roll = e.getActionCommand (); if (roll.equals ("High or Lower")) { guess = Integer.parseInt (txtguess.getText ()); static final int ANSWER = (int) (Math.random () * 100) + 1; repaint (); } } public void paint (Graphics g) { if (guess == ANSWER) { g.drawImage (smile, 50, 50, 100, 100, this); lblguess.setText ("You Got it right"); } else if (guess > ANSWER) { g.drawImage (arrow1, 50, 50, 100, 100, this); lblguess.setText ("You Got it right"); } else if (guess < ANSWER) { g.drawImage (arrow2, 50, 50, 100, 100, this); lblguess.setText ("You Got it right"); } } } |
Author: | Tony [ Wed Apr 21, 2004 10:19 pm ] |
Post subject: | |
it's not suppost to be static It's a variable as its value changes |
Author: | wtd [ Fri Apr 23, 2004 7:31 pm ] |
Post subject: | |
"static" doesn't mean "constant" in Java. Rather it means that the variable is only created once for the class, rather than being created for each instance. |
Author: | Tony [ Sat Apr 24, 2004 3:05 pm ] |
Post subject: | |
lol, I should quit flipping back and forth between languages I know little about and assuming stuff |
Author: | Dan [ Sat Apr 24, 2004 3:37 pm ] |
Post subject: | |
well tony was wrong about the static part but what he side was not wrong about the progame. it is static final int ANSWER witch means its value can not chage like he side b/c of the final seting. well at least to my knogale "final" means "constant" in java if u whont it to all ways be the same, why not make it goable and get ride of the static part? |
Author: | wtd [ Sat Apr 24, 2004 4:50 pm ] |
Post subject: | |
Definitely try removing the "static". |