lundi 29 novembre 2021

How to avoid a terminal prompt in Java

I have a simple Java program that asks a series of questions. At the end I ask the user if they would like to continue with the program or quit. If they say yes, the program asks them the same questions as before. This works, but as soon as they as asked the questions the program seems to stop. This is the function that I am calling. IS it because I don't have anything at the end of the if statement? I'm new to Java and just trying to understand this sequence.

void doMore(){

        System.out.print("Do you want to do anything else? : Yes|No ");
        String answer = keyboard.nextLine();

        if(answer.equals("YES") || answer.equals("yes")|| answer.equals("Y") || answer.equals("y")){
            showInstructions();
        }else{
            System.out.println("Thank you for using the lego manager. Please come back soon!");
            System.exit(0);
        }
}

Aucun commentaire:

Enregistrer un commentaire