Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Question: (If and Nested If) I need help pls. Ask for a password. If the pa

ID: 3795404 • Letter: J

Question

Java Question: (If and Nested If) I need help pls.

Ask for a password.  
If the password is correct then ask for an age.  The correct password is “MontyPython”.

If the age is greater than 17 print "Shangri-La welcomes you to your earthly paradise!"

If age is less than or equal to 17 print "Sorry, NO under-aged patrons allowed!"

If the password is wrong print "Invalid password!" and the program exits.

There are 4 single-selection ifs. One tests whether the password is correct. One tests whether the password is incorrect. Two are nested ifs that determine which message to print when age is greater than 17 and which message to print when age is less than or equal to 17.

Use printf() and format specifiers:  DemoPrintf.java

Exit the main().

Explanation / Answer

package print;

import java.util.Scanner;

public class DemoPrintf {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Plaese enter password ...");
       String password = scan.nextLine();
       if (password.equals("MontyPython")) {
           System.out.println("How old you are ...");
           int age = scan.nextInt();
           if (age > 17) {
               System.out.format("Shangri-La welcomes you to your earthly paradise! %d",age);
           }
           if (age <= 17) {
               System.out.format("Sorry, NO under-aged patrons allowed! %d",age);
           }
       }
       else{
           System.out.printf("Invalid password! %s",password);
       }
       scan.close();
   }


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote