我在这里遇到了一个小问题,可能是因为我不知道如何使用 try 和 catch。
我的方法有点长,但我认为问题很简单。我遇到了 input1 input2 和 input3 变量未初始化的问题 - 我无法初始化它们,因为我需要程序在未定义此变量的情况下返回错误,并且我无法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");
}