Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-If-Else-无法在对象声明中访问_Java_Class_Object_If Statement - Fatal编程技术网

Java-If-Else-无法在对象声明中访问

Java-If-Else-无法在对象声明中访问,java,class,object,if-statement,Java,Class,Object,If Statement,我如何修改此代码以使h1在内部和外部都可以访问(如果有) 没有在外面宣布,如果不是的话? 这是为了解决对象的可访问性问题,该问题严格限定在if-else语句的括号之间 我在问是否有一种方法可以使“inside”在括号外访问(我完全理解变量的作用域;所以请不要用作用域、命名约定和外部声明来打扰我)您必须将h1声明移动到外部作用域才能访问它。变量是块范围的。然后,您只需初始化if块中的变量 public class RunProg { static int input1; //for cre

我如何修改此代码以使
h1
在内部和外部都可以访问(如果有) 没有在外面宣布,如果不是的话? 这是为了解决对象的可访问性问题,该问题严格限定在if-else语句的括号之间
我在问是否有一种方法可以使“inside”在括号外访问(我完全理解变量的作用域;所以请不要用作用域、命名约定和外部声明来打扰我)

您必须将h1声明移动到外部作用域才能访问它。变量是块范围的。然后,您只需初始化if块中的变量

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);
        }

    }
或重构以删除If/Else块:

if (input1 == 1) {
    Human h1;
    ...     

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

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

}
编辑:切换到调用。等于为“是”,因为它不会为空。

如果else阻塞怎么办

Human h1 = new Human(Name, Age, Length, "YES".equals(isMarried));

把它移到外面去。它的范围仅限于
if
{}
。他的问题是如何在不将其移出的情况下完成它,我认为这是不可能的。您可以在外部声明它,但在内部实例化它,但无法回避范围问题。这也会破坏您的代码:if(isMarried==“YES”){您听说过命名约定(如JCC)吗?静态变量也不是一个好选择。
“YES”。equals(isMarried)
是一种更好的方法当然还有重构的空间。我只是解决了具体的问题。不要使用
==
进行
字符串的比较
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();

        Human h1 = new Human(Name, Age, Length, "YES".equals(isMarried));
    }
}