Solved

URGENT!!!!: Issue with Java Counter

Posted on 2014-11-16
8
214 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now