Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 hey need help wit code
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AHHNOOB




PostPosted: 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");
}
}
}
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Apr 21, 2004 10:19 pm   Post subject: (No subject)

it's not suppost to be static Confused It's a variable as its value changes Rolling Eyes
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: Fri Apr 23, 2004 7:31 pm   Post subject: (No 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.
Tony




PostPosted: Sat Apr 24, 2004 3:05 pm   Post subject: (No subject)

Confused lol, I should quit flipping back and forth between languages I know little about and assuming stuff Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dan




PostPosted: Sat Apr 24, 2004 3:37 pm   Post subject: (No 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?
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
wtd




PostPosted: Sat Apr 24, 2004 4:50 pm   Post subject: (No subject)

Definitely try removing the "static".
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: