Determine which message to print when age is greater than


If and Nested If

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().

What am I doing wrong? Whenever I run program and type the correct password "MontyPython", I get a "invalid password". I will attatch code.

My code:

import java.util.Scanner;
public class LucioMLE31
{
public static void main(String[] args)
{
String password = "MontyPython";
Scanner input = new Scanner(System.in);
int age = 0;
if (password == "MontyPython")
{
System.out.printf("What is the password:");
password = input.nextLine();

if (password != "MontyPython")
{
System.out.printf("Invalid password");
}
}
password = input.nextLine();
System.out.printf("Enter you age:");
age = input.nextInt();
if (age >= 17)
{
System.out.printf("Shangri-La welcomes you to your earthly paradise!");
//END if age>=17
}

if (age < 17)
{
System.out.printf("Sorry, NO under-aged patrons allowed!");
}//END if age<17
}

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: Determine which message to print when age is greater than
Reference No:- TGS02876072

Expected delivery within 24 Hours