1

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.");
    }
}
2
  • 1
    Please read: How to debug small programs Commented Aug 4, 2021 at 19:52
  • 3
    Well, 50 is greater than 18. So if they enter their age as 50, the age >= 18 condition is true. And what happens immediately after checking that condition, if it happens to be true? They are asked if they have a VIP pass. Or in other words, the order in which you've set your conditional statements does not reflect the requirements of the program. Commented Aug 4, 2021 at 20:01

3 Answers 3

1

I have redone your code. Incase of anything feel free to ask. This Works with all Test Cases

 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.println("Sorry, you must be over 18.");
    } else {

        if (age < 50) {
            System.out.print("Do you have a VIP pass? yes/no: ");
            String vipPassReply = keyboardInput.next();
            if (vipPassReply.equals("yes")) {
                // 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("Thanks. Go on in.");
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!! A++++++
Posting code is not enough, please explain for readers why you wrote this code and what the OP was missing in the 1st place
1

Okey just implement what you want logic in your code.

So you want the user who are +18 to are allowed to pass but with a VIP permission. But for users +50 they can pass without VIP permission.

So what you need?

You ask the user for their age. As you asked Then check if user is +18 (as you do)

But there before asking for VIP you need to check if user is +50 so if that is true you can print "You can go in". But if user user is +50 you should ask for a VIP pass (on else). Then if user answer was "YES" they can go in. Otherway you can print "You need a VIP pass to enter"

And on else when you check if user is +18 you can print "You must be +18 to enter".

Comments

1

It may be better to invert conditions: use age < 18 instead of age >= 18 and then check for 50-year limit to ask for VIP password if needed to prevent code duplication.

Also, if user input is expected, it could be preferable to use equalsIgnoreCase when checking for "yes" response.

if (age < 18) {
    System.out.println("Sorry, you must be over 18.");
}
else {
    String vipPassReply = "yes";

    if (age < 50) {
        System.out.print("Do you have a VIP pass? yes/no: ");       
        vipPassReply = keyboardInput.next();
    }
    if ("yes".equalsIgnoreCase(vipPassReply)) {
        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.");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.