lundi 30 septembre 2019

New to programming in general (JAVA). How should I fix these errors? Is the nested if-else structure correctly ordered?

I am working on a program using Java and I am getting errors. The purpose of the program is to code a while-loop with a nested if-else structure within an if-else structure. (1) There is an error on line 33 that says "variable age might not have been initialized" even though it is declared in the main()? (2) Am I correctly nesting the if-else structures inside the while? (3)Are the curly brackets correctly placed? Please let me know if I am doing anything wrong or could be doing anything better! Updated question

/**
* @author Sean Ramos
* @version1.0 9/30/2019 1:30PM 
* PURPOSE: To use a while-loop with a nested if-else structure within an if-else structure.
*/
import java.util.Scanner;
public class RamosSLE32 { //Begin class

public static void main(String[] args) { //Begin main()

Scanner input=new Scanner(System.in);
int age;
String correctPassword="MonthyPython";
char tryAgain='Y';
char tryAgain2='Y';
String q1="%nWhat is the password?";
String q2="%nEnter your age: ";

while(Character.toUpperCase(tryAgain)=='Y') {
 System.out.printf(q1);
 String password=input.nextLine();
 if(!password.equals(correctPassword))
 { 
   System.out.printf("%nInvalid password!%nDo you want to try again? Y or N.");
   tryAgain=input.nextLine().charAt(0);
 }
 else
 { if(password.equals(correctPassword))
 { System.out.printf(q2);
   age=input.nextInt();
 }
 { if(age > 17)
 {
    System.out.printf("%nShangri-La welcomes you to your earthly paradise!%nThank you! Have a nice day!");
 }
 else 
 { if (age <= 17)
 { System.out.printf("%nSorry, NO underaged patrons allowed!%nDo you want to try again? Y or N.");
   tryAgain2=input.nextLine().charAt(0);
      }
     }
    }
   }
  }
  } //End main()

  } //End class

Aucun commentaire:

Enregistrer un commentaire