mercredi 4 août 2021

Using nested If Statements in Java and ignoring conditions where applicable

I am new to Java, so apologies if this is a simple fix...

I have written a short script for a VIP lounge which implements nested if statements. The conditions are that a customer must be aged >=18 and have a VIP pass to enter, or if the customer is aged >=50 they can enter without a VIP pass. The program works fine, but if the customer enters their age as 50+, they are still being asked if they have a VIP pass, although this question is not needed if they are over 50. I have tried using a else if statement but to no avail.

Please advise. The source code follows...

public static void main(String [] args) {
    
    Scanner keyboardInput = new Scanner(System.in);
    
    System.out.println("The VIP Lounge");       
    System.out.print("Enter your age: ");
    int age = keyboardInput.nextInt();
    
    if (age >= 18) {
        System.out.print("Do you have a VIP pass? yes/no: ");       
        String vipPassReply = keyboardInput.next();
        
        if (vipPassReply.equals("yes") || age >= 50) {
            // over 50's can enter the VIP lounge without a pass
            System.out.println("Thanks. Go on in.");
        }
        else {
            System.out.println("Sorry, you must either be over 50, or have a VIP pass to enter.");
        }
    }
    else {
        System.out.println("Sorry, you must be over 18.");
    }
}

Aucun commentaire:

Enregistrer un commentaire