Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default 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.

    Java Code:
    if(cardOne == cardFive)
    {
    	score++;
    }
    but evidently I'm wrong because it's not working. All help appreciated.

  2. #2
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Score Counter In Java GUI.

    where did you initialized the score? Is it an instance variable, local variable or class variable?

  3. #3
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default 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);
    Java 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...

  4. #4
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default 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 04:51 PM. Reason: too long winded :p

  5. #5
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default 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);
    }

  6. #6
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default 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.

  7. #7
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default 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!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    30
    Rep Power
    0

    Default Re: Score Counter In Java GUI.

    Quote Originally Posted by JosAH View Post
    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?

  10. #10
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default 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.

Similar Threads

  1. Replies: 4
    Last Post: 02-16-2011, 12:05 AM
  2. solr score
    By belsen in forum Lucene
    Replies: 0
    Last Post: 11-16-2010, 04:04 PM
  3. How to do a score board?
    By vlan in forum Java Applets
    Replies: 11
    Last Post: 06-03-2010, 11:10 AM
  4. Klaverjas Score 0.2
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-23-2007, 08:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •