dimanche 27 mai 2018

Checking if user input equals a certain string always evaluates to false

I'm trying to implement a small game, and the user has the option to choose whether or not proceed. The Scanner should read the input and compares the choice to see whether or not do so.

public static void main(String[] args){
    System.out.println("This is agame where you fight 7 bosses one after another, using nothing but the weapons that you have on you.");
    System.out.println("Progress will not be saved, you must fight them all in one shot.");
    System.out.println("Don't worry, your health and damage will increase after each fight.");

    Scanner readChoice = new Scanner(System.in);
    System.out.println("Are you up for the challenge? Enter no to leave, anything else to play");
    String playerChoice = readChoice.next();

    if ("no".equals(playerChoice)){
        System.out.println("Oh, well, come back when you feel you are ready.");
        System.exit(0);
    }
    else{
        beginGame();
    }

    readChoice.close();
}

However, the method beginGame() is always called even when the string "no" is entered. I'm not sure why the if keeps evaluating to false. I'm following the advice from the link below, but it still doesn't solve anything for me.

Reading and checking strings from user input

Aucun commentaire:

Enregistrer un commentaire