dimanche 9 février 2020

Java Must you declare keyboard.next() in an if, else if statement?

To start, I've only been programming for about a month, please go easy on me.

I want to have a program check if a keyboard entry is either "y" or "yes" while ignoring case. If either are true, I want to continue the program, otherwise Exit.

I have tried using || and it did not work, now I am trying to split the code into if and else if then else but this requires multiple entries for the keyboard.next()

Do I have to create a string just to check for this Yes/No situation? Here's my current code:

public class yesNo {
        public static void main(String[] args) {

        if (keyboard.next().equalsIgnoreCase("y")) {
            System.out.print("Correct");
        } else if (keyboard.next().equalsIgnoreCase("yes")) {
            System.out.print("Correct");
        } else {
            System.exit(0);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire