Solved

URGENT!!!!: Issue with Java Counter

Posted on 2014-11-16
8
224 Views
Last Modified: 2014-11-17
Hi,

I have this code here that runs an guessing game for flags.

The user has 3 tries to guess the correct flag. If they guess it correct on the first try, they get 3 points and since it was only 1 guess, I added a counter (numGuesses) to count the number of guesses on the first try they have correct.

I am having an issue. See the method around line 261 (private void submitGuess(Button guessButton) )

The counter seems to be inaccurate and don't know what I did wrong? Either on the last try it does not add the numbers correctly or on the first try.

Where am I messing up?

Thanks!
source.java
0
Comment
Question by:Computer Guy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 40446331
So what result do you get, and which var are you referring to as being wrong?
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40446364
CorrectFirstTry is short we of the time and the same with points.  Not sure if it is calculating the last since it is a loop of 10.
0
 
LVL 27

Accepted Solution

by:
dpearson earned 250 total points
ID: 40446388
Your comment at the top suggests you're counting the number of tries to get the question right:

   private int numGuesses; // number of guesses on current question


But the code in submitGuess() only increases the "numGuesses" when the guess is correct.
Is that what you intended?
 
      // if the guess is correct
      if (guess.equals(answer))    
      {
         ++correctAnswers;
         ++numGuesses;  // Should this be before the 'if' statement?

If that's not the problem - you can debug this by logging what the value of numGuesses is each time through.  Either with
System.out.println("numGuesses is " + numGuesses) or using the Log.e() logger and seeing how it changes compared to what you expect.

Hope that helps,

Doug
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:Computer Guy
ID: 40446423
My thought was numGuesses would be less than 3. Then it resets with next question.

If the first try is correct the number guess is 1. And then correctForstTry would be +1 and points would be 3.
0
 
LVL 3

Author Comment

by:Computer Guy
ID: 40446452
I tried adding that code and it does not output in the console. This is an android app but I still don't see it on the console.
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40446454
We don't really know what the problem is meant to be doing - so it's hard to help you debug it.  It's clear you get points for correct answers, but exactly how many points and when isn't clear.

If you want help, I think you need a clearer explanation of exactly which variables have the wrong values and when.

Did you try my suggestion of logging the value of "numGuesses" and watching how it changes with each guess?
What values do you get, what points and what do you expect?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40446944
I am pleased doug made the point about numGuesses as that was my first thought. Whether it's the problem or not, it should not be inside any if statement afaics.
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 250 total points
ID: 40446991
Somehow, and schematically, I'd say that one approach would be static ints for totalGuesses, points and correctAnswers, which are updated by a Runnable which is instantiated per-new-flag-challenge.

In the Runnable you could loop the three attempts, and have numGuesses in there (if you think you need it), but in any case update totalGuesses from there. In your main class you can deliver a new Runnable as soon as the previous one returns, and you can hold this over by using a wait() notify() or other more modern semaphore type.

Even ignoring this suggestion, the management of your points and guesses vars should be straightforward - since you know your code best, you should follow what doug has said and monitor the states of those counters as a first step.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 53
Programming Codes 2 41
Hide file extensions in Notepad++ tabs 2 49
Samsung Dex 3 45
Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question