-2

so, for my Java class I need to make a HiLo game. I figured the game out on my own, but I wanted to take it a step further, and after the user completes the game(guesses the right number) I want to ask them if they would like to play again. The problem is, I can't seem to figure out exactly how. I have tried several things and at the moment I'm trying to get them to enter either yes or no to continue playing the game. If someone could help me figure out what I'm doing wrong and explain it to me, that would be great. Thanks!

    Random generator = new Random();
    Scanner scan = new Scanner(System.in);
    int answer, guess; 


    answer = generator.nextInt(101);
    System.out.println("Lets play a game. Guess the number(0-100): ");
    guess = scan.nextInt();

    while (guess != answer){
        System.out.println("Wrong guess.");
        if (guess > answer){
            System.out.println("Your guess was higher than the answer. Guess again: ");
        }
        else{
            System.out.println("Your guess was lower than the answer. Guess again: ");
        }
        guess = scan.nextInt();
    }
    if (guess == answer)
        System.out.println("You got it! The number was " + answer);

    //after initial game finishes, we ask if they want to play again.
    System.out.print("Want to play again? ");
    String again = scan.nextLine();

    while (again != "no"){
    System.out.println();
    System.out.print("Great! lets play again.");
    System.out.println("Take a guess(0-100 inclusive): ");
    guess = scan.nextInt();
    while (guess != answer){
        System.out.println("Wrong guess.");
        if (guess > answer){
        System.out.println("Your guess was higher than the answer. Guess again: ");
    }
        else{
        System.out.println("Your guess was lower than the answer. Guess again: ");
    }
    guess = scan.nextInt();

    if (guess == answer)
        System.out.println("Your got it! The number was " + answer);

    System.out.println("Want to play again? (0 for no, 1 for yes): ");
    again = scan.nextLine();
    }
    }
    System.out.println("Thanks for playing!");
}

}

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
oMatt
  • 1
  • 1

1 Answers1

0

The != operator does not check if two strings contain the same characters. What you want to do is:

while (again.equals("yes")) {
  ...
}

You should also try to put your game code inside another loop, so at the end of the game (still inside the loop) you can ask the player if they want to play again. If yes, the loop has to be executed again, otherwise it stops.

This way the player can play an infinite number of games and you don't need to duplicate your game code. Example (to get an idea of what the structure could look like):

// start with "yes" in order to enter the game loop initially
String again = "yes";

while(again.equals("yes")) {
    answer = generator.nextInt(101);
    System.out.println("Lets play a game. Guess the number(0-100): ");
    guess = scan.nextInt();

    while (guess != answer){
        System.out.println("Wrong guess.");
        if (guess > answer){
            System.out.println("Your guess was higher than the answer. Guess again: ");
        }
        else{
            System.out.println("Your guess was lower than the answer. Guess again: ");
        }
        guess = scan.nextInt();
    }

    System.out.println("You got it! The number was " + answer);
    // ask them if they want to play again. only "yes" will start another game
    System.out.println("Do you want to play again (yes/no)?");
    again = scan.nextLine();
}
Sky
  • 674
  • 8
  • 22
  • I implemented what you suggested. However, I can't get the loop to work properly. Currently what I have is: while (again.equals"yes"){ while (guess != answer){ game code } System.out.println("Would you like to play again?); again = scan.nextLine(); } And it just ends the program without asking them to play again, and thanks them for playing – oMatt Oct 06 '16 at 17:49
  • @oMatt I've updated my answer with an example loop. Try something similar and I think you'll get it working. Good luck :) – Sky Oct 07 '16 at 11:01