Results 1 to 5 of 5
Thread: guessing game help
- 10-30-2009, 09:38 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 23
- Rep Power
- 0
- 10-31-2009, 03:52 AM #2
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
As written, the declarations of min and max are inside the actionPerformed() method.
Their value will be initialized as the declarations are evaluated every time actionPerformed() is called.
They will only be initialized once if the declarations are moved outside, but still within class Action:
Java Code:static class Action implements ActionListener { [COLOR="Navy"] int max, min, guess; boolean guessed; guessed = false; max = 1000; min = 0; guess = (0 + 1000) / 2;[/COLOR] public void actionPerformed(ActionEvent e) { ...
- 10-31-2009, 12:59 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 23
- Rep Power
- 0
thank you for your suggestion but
i get the three errors in the follwoing areas highlighted in red,
i tried fixing it by adding open curly braces after "boolean guessed; {" and close curly bracket after " guess = (0 + 1000) / 2;}" which removes all the errors but the functionality does not work properly.
any suggestion again :D
buttonCorrect.addActionListener(a);
panel2.add(label);
}
});
}
static class Action implements ActionListener {
int max, min, guess;
boolean guessed;
guessed = false;
max = 1000;
min = 0;
guess = (0 + 1000) / 2;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonHigh) {
min = guess;
- 10-31-2009, 03:51 PM #4
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
use {...} or initializers
Perhaps I shouldn't try to write these things in the middle of the night. :(
Of course it won't compile; I should not have put assignment statements among the class declarations.
Your fix was on the right track, but did not go far enough;
all four assigments would have to be inside the {...}
Another approach is initializers; they are allowed even though they look almost like assignments:
Java Code:static class Action implements ActionListener { int max = 1000, min = 0; [COLOR=Green]// initializers are legal[/COLOR] int guess = (min+max) / 2; boolean guessed = false; // max = 1000; min = 0; [COLOR=Red]// assignments are illegal[/COLOR] public void actionPerformed(ActionEvent e) { ...
- 10-31-2009, 05:37 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
guessing game using GUI
By yasmin k in forum New To JavaReplies: 1Last Post: 10-26-2009, 12:13 PM -
Game 21
By aRTx in forum Advanced JavaReplies: 3Last Post: 04-04-2009, 12:33 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM -
Tic Tac Game
By loggen in forum New To JavaReplies: 1Last Post: 12-12-2008, 07:36 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks