0

I am working on a guessing number game and I am having problems setting up the loop that gives the player the option to play the game again. I was also wondering if there is a way to include a hints option inside the loop.

I have been programming for 2 months.

package carlosnumber;

import java.util.Random;
import java.util.Scanner;

public class CarlosNumber {

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        String name = "start";
        int number = 0;
        int numTries = 0;
        int secretNumber;
        String wantHints, playagain;
        int guess = -1, numGuess, wonGames, quitGames, playedGames;
        boolean end = false;
        boolean hint = false;
        boolean play = true;
        String win = null;

        System.out.println("Welcome the the Guessing game");

        while (play == true) {
            do {
                Random secretNumberGenerator = new Random();
                secretNumber = 56;

                if (name.equals("start")) {
                    System.out.println("To Start playing, please enter your name");
                    name = console.nextLine();

                    System.out.println("The system will randomly select a number between 1 and 100"
                            + "\nYour goal is to guess the number. At each turn enter"
                            + " in your guess, and I will tell you when your guess is correct");

                }
                System.out.println("Please Enter a number");
                number = console.nextInt();
                numTries++;

                if (number < secretNumber) {
                    System.out.println("Your guess is low");
                }
                if (number > secretNumber) {
                    System.out.println("Your guess is high");
                }
                if (number == secretNumber) {
                    System.out.println("YOU WIN!!" + "It took you " + numTries + " Tries");
                    end = true;
                }

            } while (end == false);

            System.out.println("Do you wish to play again? Y/N");
            playagain = console.nextLine();

            if (playagain == "Yes") {
                play = true;
            } else {
                play = false;
            }

        }
    }
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
  • 3
    `playagain == "Yes"` is not how you do `String` comparison, instead you should be using something like `"Yes".equals(playagain)` – MadProgrammer Oct 14 '15 at 04:47
  • Welcome to SO. Please read on that link provided by @MadProgrammer as it is very useful especially to someone that's just starting their Java programming. As for how this site works, please go to the [tour](http://stackoverflow.com/tour). – Keale Oct 14 '15 at 04:52
  • @MadProgrammer is this really a duplicate? Yes his first question is because of his string comparison but what about the second question? – Keale Oct 14 '15 at 04:57
  • 1
    @Keale Well, there in lies a problem, the OP should asking a single, concise question, not multiple questions. So, now they can try and fix their first issue and ask another question regarding their second question – MadProgrammer Oct 14 '15 at 04:59
  • @MadProgrammer Thanks for the help and yes I added playagain.equalsIgnoreCase("Yes") and it worked! Now I have another question. If i wanted to add hints as a yes/no option, at the start of the program, would i need to set up two different paths for the comparason of the numbers? or can I do a loop and have the system.print not print?. – Carlos Oct 15 '15 at 05:35

0 Answers0