0

I made a basic number guessing game, everything was working fine until i tried to add a "play again" feature at the end of it. When the program is run, after inputting the first guess, it just starts the loop over again without going through the rest of it. Also, I am unsure if my code is efficient or not. It seems like too much coding for a simple concept. Is this an average length for a basic guessing program? Sorry if my questions are worded strangely. I'm a first year college student just learning the basics of programming. Here is my code:

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

    boolean playing = true;
    do {
        int max = 100;
        int min = 1;
        int counter = 10;
        int guess = 0;

        int guessThis = min + randomNum.nextInt(max);
        System.out.println("I'm thinking of a number between 1 and 100. You have 10 tries to guess it. What's your first guess?");

        guess = input.nextInt();
        counter--;

        if (guess == guessThis) {
            playing = false;
        } else {
            playing = true;
        }
        if (guess > max) {
            System.out.println("I said the number is between 1 and 100. You think this is a GAME MUTHA FUCKA??! Guess again... :) " + counter + " guesses left.");  
        }
        if (min > guess) {
            System.out.println("Bruh. Are you stupid? " + guess + " is not between 1 and 100. Try again dummy boi. " + counter + " guesses left.");
        }   
        if (guess > guessThis && min <= guess && guess >= max && playing == true && counter > 0) {
            System.out.println("Too high. Guess again. " + counter + " guesses left.");
        } else if (guess < guessThis && min <= guess && guess >= max && playing == true && counter >0) {
            System.out.println("Too low. Guess again. " + counter + " guesses left.");  
        } 
        if (playing == false && counter > 0) {
            System.out.println("You guessed it!");
        }
        if (counter <= 0) {
            System.out.println("You lose! Ha! Fuck off broooooo. My number was " + guessThis);
            playing = false;
        }   


    }while (playing == true);

    String answer;

    if (playing == false) {
        System.out.println("Wanna play again? (y/n)");
    }
    answer = input.next();
    if (answer == "n") {
        System.out.println("My game isn't fun enough for you? Wow, okay, rude. Bye then. Dh.");
        input.close();
    } if (answer == "y") {
        playing = true;
    }
}

}

ajc
  • 21
  • 5

1 Answers1

-1

use

 }while (playing == true);

after the end of if statement

if (answer == "y") {
    playing = true;



}
Harsh Mishra
  • 1,975
  • 12
  • 15
  • I moved the }while (playing == true); down to after the end of the if statement, but now the code just stops after inputting the first guess input – ajc Mar 07 '19 at 05:32