变量未初始化,我不能使用null作为布尔值

wil*_*lem 0 java boolean compiler-errors try-catch

我在这里遇到一个小问题可能是因为我无法弄清楚如何使用try和catch.

我的方法有点长,但问题很简单.我有输入1输入2和输入3的变量没有初始化的问题 - 我无法初始化它们,因为我需要程序返回错误,以防这个变量没有定义,我不能再把它放在null能够找到这个错误.

如何进行?

public void print() {
    boolean input1;
    boolean input2;
    boolean input3;
    String gate;
    boolean calc;

    for (String a : operations.keySet()) {
        if (Gate2.contains(operations.get(a).Gate)) {
            if (inputs.containsKey(operations.get(a).input1)) {
                input1 = inputs.get(operations.get(a).input1);
            }
            if (inputs.containsKey(operations.get(a).input2)) {
                input2 = inputs.get(operations.get(a).input2);
            }
            if (result.containsKey(operations.get(a).input1)) {
                input1 = result.get(operations.get(a).input1);
            }
            if (result.containsKey(operations.get(a).input2)) {
                input2 = result.get(operations.get(a).input2);
            }
            gate = operations.get(a).Gate;
            calc = cc.calc(input1, input2, gate);
            result.put(a, calc);

        }else if (Gate3.contains(operations.get(a).Gate)) {
            if (inputs.containsKey(operations.get(a).input1)) {
                input1 = inputs.get(operations.get(a).input1);
            }
            if (inputs.containsKey(operations.get(a).input2)) {
                input2 = inputs.get(operations.get(a).input2);
            }
            if (inputs.containsKey(operations.get(a).input3)) {
                input3 = inputs.get(operations.get(a).input3);
            }
            if (result.containsKey(operations.get(a).input1)) {
                input1 = result.get(operations.get(a).input1);
            }
            if (result.containsKey(operations.get(a).input2)) {
                input2 = result.get(operations.get(a).input2);
            }
            if (result.containsKey(operations.get(a).input3)) {
                input3 = result.get(operations.get(a).input3);
            }
            gate = operations.get(a).Gate;
            calc = cc.calc(input1,input2,input3, gate);
            result.put(a, calc);

        }else if ("not".equals(operations.get(a).Gate)) {
            if (inputs.containsKey(operations.get(a).input1)) {
                input1 = inputs.get(operations.get(a).input1);
            }

            if (result.containsKey(operations.get(a).input1)) {
                input1 = result.get(operations.get(a).input1);
            }

            gate = operations.get(a).Gate;
            calc = cc.calc(input1, gate);
            result.put(a, calc);
        }

    }

    System.out.format(" Inputs --> ");
    for (String a : inputs.keySet()) {

        int bit2 = inputs.get(a) ? 1 : 0;
        System.out.format("%5s", bit2);
    }
    System.out.format(" Results --> ");
    for (String a : result.keySet()) {

        int bit2 = result.get(a) ? 1 : 0;
        System.out.format("%3s", bit2);
    }
    System.out.format("\n");

}
Run Code Online (Sandbox Code Playgroud)

Nan*_*ale 5

您正在使用原始布尔值,因此您需要将其初始化为true或false.您可以使用包装器java.lang.Boolean.