samedi 28 novembre 2020

java if/else statements always runs else statement [duplicate]

I have to make a program for class and for some reason my if/else statements will always skip straight to the else despite fitting inside the parameters to run the if or else if statements. For example, in the code below, when given the input "Yes" or "yes" it always returns "Bad input".

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Do you want to continue? ");
        String ans = s.nextLine();
        ans = ans.toLowerCase();
        if(ans=="yes"||ans=="y"||ans=="ok"||ans=="sure"||ans=="why not") {
            System.out.print("OK");
        } else if(ans=="n"||ans=="no") {
            System.out.print("Terminating");
        } else {
            System.out.print("Bad input");
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire