#Java beginner, elseif not initializing

7 messages · Page 1 of 1 (latest)

thin talon
#

Hey I started learning java today and decided to recreate some easy python projects to get a hang of the syntax. I'm confused with why my if and elif aren't being used here though when I input easy or hard.

zealous flaxBOT
#

This post has been reserved for your question.

Hey @thin talon! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

thin talon
#
package numberguessgame;
import java.io.File;
import java.util.Scanner;

public class GuessingGame {
    public static void guessingNumber() {
        System.out.println("Welcome to the Number Guessing Game!" );
        boolean gameRunner = true;

        if (gameRunner) {
            Scanner myDifficulty = new Scanner(System.in);
            System.out.println("Choose a difficulty: Easy or Hard");
            String gameDifficulty = myDifficulty.nextLine();
            gameDifficulty = gameDifficulty.toLowerCase(); // normalizing
            System.out.println(gameDifficulty);

            if (gameDifficulty == "easy") {
                int guessesRemaining = 10;
                System.out.println("Working");

            }
            else if(gameDifficulty == "hard") {
                System.out.println("Working");
                int guessesRemaining = 5;

                Scanner myGuess = new Scanner(System.in);
                System.out.println("Choose a number between 1-100: ");
                int numGuess = myGuess.nextInt();
                System.out.println(numGuess);
                gameRunner = false;
            }
        }
    }

    public static void resultsCompare(){
        System.out.println("Test");
    }




    public static void main(String[] args){
        guessingNumber();
    }
}


zealous flaxBOT
#

Looks like you're having some trouble comparing strings, check out this stackoverflow question for help.

thin talon
#

thanks, @supple haven

zealous flaxBOT
# thin talon thanks, <@628241756235890708>

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.