samedi 20 août 2016

Java - If-Else - inaccessible inside-object declaration

public class RunProg {
    static int input1; //for creating new human;
    static boolean Married; //for knowing if married or not
    static String isMarried; //for the YES or No of Married Boolean

public static void main(String[] args) {


    Scanner input = new Scanner(System.in);


    System.out.print("What would you like to do? \n 1-Create new Human?\n");
    input1 = input.nextInt();
    input.nextLine();

    //Inputs for The NEW HUMAN
    if (input1 == 1) {
        System.out.println("Enter name:");
        String Name = input.nextLine();

        System.out.println("Good! Now enter the age of " + Name + ": ");
        int Age = input.nextInt();
        System.out.println("Well done! Now enter length: ");
        int Length = input.nextInt();
        input.nextLine();

        System.out.println("Finally, is he/she married? ('YES' or 'NO') ");
        isMarried = input.nextLine();


        if (isMarried == "YES") {
            Married = true;
            Human h1 = new Human(Name, Age, Length, Married);
        }

         else if (isMarried == "NO") {
            Married = false;
            Human h1 = new Human(Name, Age, Length);
        }

    }

How can I modify this code to make h1 accessible inside and outside if-else without declaring it outside if-else?

Aucun commentaire:

Enregistrer un commentaire