-1

I have a guessing program, and Im trying to add a counter. In the bolded parts I have a counter. When you get a guess wrong, it adds 1 to the counter. But when it prints it, it just prints as 0 no matter what.

                    public static void firstGame(String Answer, String userGuess, int counter)
                {
                    userInput = new Scanner(System.in);

                    boolean playgame = true;

                    while(playgame == true)
                    {
                    Answer = "Sam";

                    lnPrint(" ");
                    lnPrint("Take a guess at my name! It starts with an S...");
                    sPrint(":");
                    userGuess = userInput.next();

                    if(userGuess.equalsIgnoreCase(Answer))
                        {
                            lnPrint("You got it! Its " + Answer);
                            lnPrint(" ");
                            break;
                        }

                    else if(userGuess != Answer)
                        {
                            lnPrint("Good guess, keep trying!");
                            counter++;
                        }

                    }
                }

That is my game method with the counter.

public static void main(String[] args)
    {
        Scanner userInput = new Scanner(System.in);

        int Start, counter = 0;
        String Answer, userGuess, middleAnswer, middleUserGuess;
        Answer = null;
        userGuess = null;
        middleAnswer = null;
        middleUserGuess = null;
        Start = 0;

            while(Start !=2)
            {

            lnPrint("(1) Start Game");
            lnPrint("(2) Exit");
            sPrint(":");
            Start = userInput.nextInt();

                if(Start == 1)
                    {
                        firstGame(userGuess, Answer, counter);
                        lnPrint("Now, how about my middle name?");
                        nlnPrint(counter);
                        middleGame(middleAnswer, middleUserGuess);

                    }

This is the code that prints it

Whatty
  • 79
  • 2
  • 5

2 Answers2

0

This looks like a common pass by value / reference problem. When using counter as an argument for firstGame, it's the value of counter that is used in the firstGame method, as opposed to passing a reference to the counter variable; you can read about this here, for example: http://www.javacoffeebreak.com/faq/faq0066.html

There are ways of passing integers by reference to methods (see Java : Best way to pass int by reference) but in this case I think you should simply declare counter as a global variable.

Community
  • 1
  • 1
  • I tried making it global, its now giving me an error saying, "Illegal modifier for parameter counter; only final is permitted" I put the global int inside the main method. – Whatty Nov 14 '14 at 02:36
-1

Have you initialized "counter" earlier? Heres an example where it works:

$.Guess = function() {
    var parent = this;
    var counter = 0;

    this.guess = function(val) {
        if ( val != 5 ) {
            parent.guessedWrong();
        }
    }
    this.guessedWrong = function() {
        counter++;
    }
    this.guessed = function() {
        return counter;
    }
    this.resetCounter = function() {
        counter = 0;
    }
}


var game = new $.Guess();
game.guess(1);
game.guess(2);
alert( game.guessed() );

game.guess(3);
game.guess(4);
game.guess(5);
// Alerts 4, because there is 5 guesses and one of them (the one with value 5) is correct
alert( game.guessed() );
// Reset counter to 0
game.resetCounter();

game.guess(6);
game.guess(7);
game.guess(8);
alert( game.guessed() );

..the code above can be tested here: http://jsfiddle.net/jcpjr9dc/

Kirbo
  • 381
  • 1
  • 12
  • I have initiated counter in the main method. And Im not sure on what your code means or does. Is there a way where I can use it in my code without changing it all? – Whatty Nov 14 '14 at 01:35
  • Now that I paid more attention for your code, seems like its Java? My bad, I didn't notice it earlier, took a fast glimpse of it and thought it was javascript. But I still think my example is valid, no matter which language is used (correct me if I'm wrong). – Kirbo Nov 14 '14 at 01:41
  • Yes it is Java. But that code isnt something Im looking for. I need to know why the counter++; in a method isnt working the main method. I have it initialized to 0 in the main method also. – Whatty Nov 14 '14 at 01:42
  • Well, I'm sorry I can't help you with that, I have no experience with Java :/ One hot tip: you could have added tag "java" :) – Kirbo Nov 14 '14 at 01:45