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.
Code:
if(cardOne == cardFive)
{
score++;
}
but evidently I'm wrong because it's not working. All help appreciated.
Re: Score Counter In Java GUI.
where did you initialized the score? Is it an instance variable, local variable or class variable?
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.
Code:
public class guessingGame extends JFrame implements ActionListener
{
//Declare the score and cards.
int score = 0;
Code:
JLabel scoreLabel = new JLabel("Score: "+score);
Code:
if (lastClicked != null && ((JButton)(e.getSource())).getText() == lastClicked.getText())
{
score ++;
scoreLabel.setText("Score: " + score);
}
I tried making a new card (JButton) and tried to get it to work that way but no luck...
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.
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);
}
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.
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!
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,
Jos
Re: Score Counter In Java GUI.
Quote:
Originally Posted by
JosAH
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,
Jos
Yeah I think you're right. UnAccomplishedJavaPerson, do your card objects have an attribute that you can compare? You're comparing the objects themselves, and unless they're a primitive type it's not going to work.
What type are they?
Re: Score Counter In Java GUI.
Sorry for such a late reply, but I had each card declared as a letter...in the end I never got it to work but thanks for everyones help, I'll ask the lecturer when it comes to it.