Solved

URGENT!!!!: Issue with Java Counter

Posted on 2014-11-16
8
228 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to implement Singleton Design Pattern in Java.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

738 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