lundi 29 mai 2017

If no conditions are met, return message

So I'm really new to Java, and what I'm trying to do is let the user input a keyword, which will be passed to different conditions. If the keyword starts with "c", it will execute cmethod. If the keyword starts with "n", it will execute nmethod. The problem is, I also want to display a message if the user inputs neither something that starts with "c" or "n". Here's what I've been able to think through (I'm aware that this code won't work though)

    public static void main(String[] args) {
    System.out.println("This program can randomize characters or numbers.");
    System.out.println("Please input \"c\" for characters, and \"n\" for numbers. ");
    Scanner choices = new Scanner(System.in);

    if (choices.next().startsWith("c")) {
        cmethod();
    }

    else {
        nmethod();
    }

    //how do I execute the code below if none of the conditions are met?
    else {
        System.out.println("Keyword is wrong.");
    }
}

Aucun commentaire:

Enregistrer un commentaire