Results 1 to 10 of 10
Thread: Score Counter In Java GUI.
- 11-07-2011, 10:50 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Score Counter In Java GUI.
I have eight cards, and I need to match them up and keep track of the score, getting 1 point for each correct match. I have the score initialised, and ready to print out(I think), but I can't seem to increment it?
For example lets say card one and card five match, I have the likes of below in the action event part of my program.
but evidently I'm wrong because it's not working. All help appreciated.Java Code:if(cardOne == cardFive) { score++; }
- 11-07-2011, 11:45 AM #2
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Score Counter In Java GUI.
where did you initialized the score? Is it an instance variable, local variable or class variable?
- 11-07-2011, 01:51 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Re: Score Counter In Java GUI.
As a class variable, so it can be used in all methods in including the action listener? Basically have it declared as soon as I started the program along with my cards, I had a different idea but the score count still won't work, here's some code I have.
Java Code:public class guessingGame extends JFrame implements ActionListener { //Declare the score and cards. int score = 0;Java Code:JLabel scoreLabel = new JLabel("Score: "+score);I tried making a new card (JButton) and tried to get it to work that way but no luck...Java Code:if (lastClicked != null && ((JButton)(e.getSource())).getText() == lastClicked.getText()) { score ++; scoreLabel.setText("Score: " + score); }
- 11-07-2011, 03:48 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Score Counter In Java GUI.
What you have there is an instance variable, so the values are unique to each object of guessingGame created. If you want a class variable try putting "static" (without the quotes) tag before the primitive type int.
Last edited by CHLim; 11-07-2011 at 03:51 PM. Reason: too long winded :p
- 11-07-2011, 04:33 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Re: Score Counter In Java GUI.
Okay so I have it set as
static int score = 0;
so that's declared and initialised now, and then in the action listener part of the program, I'm supposed to do something to make the score count using score++...I've been through many ideas and at the moment I'm on the one where I tell the program which one matches and if they're clicked together make the score go up, but still no luck...
if(e.getSource()==cardOne && e.getSource()==cardFive)
{
score ++;
scoreLabel.setText("Score: " + score);
}
- 11-07-2011, 04:40 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Score Counter In Java GUI.
Hmm ic. Than it sounds like a bug in your program.
why don't you try printing out e.getSource() to check if those values are correct.
- 11-07-2011, 04:47 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Re: Score Counter In Java GUI.
Bugs in this program will be the death of me :/ I know which cards match, it's just wrecking my head now, and I have 15 minutes to finish it! I think I might just call it a day seeing as I have most the program working, however if anyone happens to know the answer, let me know!
- 11-07-2011, 05:38 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Score Counter In Java GUI.
I think you're mistakenly assuming that you can compare non-primitive variables for equality with the == operator. You have to use the equals( ... ) method instead and possibly you have to implement it for your own class(es).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-07-2011, 05:46 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 31
- Rep Power
- 0
- 11-18-2011, 01:47 PM #10
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
I need a java program that takes scores and ouputs average, highest, and lowest score
By TheSweetHelloKitty in forum New To JavaReplies: 4Last Post: 02-15-2011, 11:05 PM -
solr score
By belsen in forum LuceneReplies: 0Last Post: 11-16-2010, 03:04 PM -
How to do a score board?
By vlan in forum Java AppletsReplies: 11Last Post: 06-03-2010, 10:10 AM -
Klaverjas Score 0.2
By levent in forum Java SoftwareReplies: 0Last Post: 05-23-2007, 07:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks