Solved

URGENT!!!!: Issue with Java Counter

Posted on 2014-11-16
8
220 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
  • 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 26

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 26

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 40
check java version using powershell 13 92
maven module vs maven project 3 23
JAVA API design with micro service cloud in mind 1 16
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

773 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