1

I made some command line games and at the end of the game I want to ask if the player wants to play again. You can see in the code how I made but it's not working and I don't know why.

Can anybody help me?

import java.util.Scanner;
//WIP
public class GuessingGame2 {

    static Scanner userInput = new Scanner(System.in);

    static int randNumber;
    static int guessNumber;
    static boolean gameStatus;

    static void checkNum(int x) {


        guessNumber++;

        if(x == randNumber) {
            gameStatus = true;
        } else if(x < randNumber) {
            System.out.println("Too small!");
        } else {
            System.out.println("Too big!");
        }       
    }

    static void checkAnsw() {

        if(userInput.hasNextLine()) {

            if(userInput.nextLine() == "Y" || userInput.nextLine() == "y") {
                guessGame();
            } else if(userInput.nextLine() == "N" || userInput.nextLine() == "n") {

            } else {
                System.out.print("Y or N ");
                checkAnsw();
            }

        } else {
            System.out.print("Y or N ");
            userInput.next();
            checkAnsw();
        }
    }

    static void guessGame() {

        randNumber = (int) (Math.random() * 1000);
        guessNumber = 0;
        gameStatus = false;

        System.out.println("Try to guess a number from 1 to 1000!");

        while(gameStatus == false) {

            System.out.print("Your guess: ");

            if(userInput.hasNextInt()) {
                checkNum(userInput.nextInt());
            } else {
                System.out.println("You need to choose a number!");
                userInput.next();
            }
        }

        System.out.println("You guessed the number in " + guessNumber + "  tries.");
        System.out.print("Do you want to play again? Y or N ");

        checkAnsw();

    }

    public static void main(String[] args) {

        guessGame();

    }
}
Darshan Patel
  • 2,839
  • 2
  • 25
  • 38
Blaz
  • 123
  • 6
  • 1
    Your string comparison is wrong. [Check this](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Also try to change your design. You have the possibility of getting stack overflow error since your having recursion in your code between `checkAnsw()` and `guessGame()`. – Codebender Jul 25 '15 at 16:30
  • @Codebender harsh! given the style of coding and the game itself (looks fun btw!), I'm guessing this guy is a coding-novice. Let him have this one - stackoverflows and other problems can be sorted out later :-) – adelphus Jul 25 '15 at 16:45
  • @adelphus I think you're forgetting the point of StackOverflow. This should actually be closed for being off-topic (requesting debugging help) – Vince Jul 25 '15 at 17:23

2 Answers2

3

Change your checkAnsw method to this:

static void checkAnsw() {

if(userInput.hasNextLine()) {

    if(userInput.nextLine().equalsIgnoreCase("y")) {
        guessGame();
    } else if(userInput.nextLine().equalsIgnoreCase("n")) {

    } else {
        System.out.print("Y or N ");
        checkAnsw();
    }

    } else {
        System.out.print("Y or N ");
        userInput.next();
        checkAnsw();
    }
}

You cannot compare Strings with the = as they are objects. Use the .equals method to compare Strings.

jchamp
  • 172
  • 3
  • 11
  • This still has a recursive problem, but is getting closer to what OP probably wants. Upvote. – Dave Jul 25 '15 at 16:42
  • That's true. It may be better to replace the `guessGame` method with a loop in `main` that does the same thing, if OP's requirements allow it. – jchamp Jul 25 '15 at 16:50
0

Your code works fine, I have copied and pasted it in Eclipse and this is the output:

Try to guess a number from 1 to 1000!
Your guess: eeeee
You need to choose a number!
Your guess: 1
Too small!
Your guess: 2
Too small!
Your guess: 2

I don't understand which is your problem, try to explain better

navy1978
  • 1,411
  • 1
  • 15
  • 35
  • 1
    The problem is not when the game is being played - it's when the game is over and you need to choose whether to continue or not. – adelphus Jul 25 '15 at 16:37