Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

URGENT!!!!: Issue with Java Counter

Posted on 2014-11-16
8
Medium Priority
?
234 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 28

Accepted Solution

by:
dpearson earned 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 28

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 1000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Here, we have shared an easy step by step tutorial on how to download instagram images and videos on PC, Android Mobile and iOS mobile.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Starting up a Project
Suggested Courses

719 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