I'm with a little problem here probably is because I cant figure out how to use try and catch.
My method is a bit long but the problem is simple I think. I'm having the problem of variable not initialized for the input1 input2 and input3 - I cant initialize them because I need the program to return a error in case this variables is not defined and I cant put then on null
to be able to find this error.
How to proceed?
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");
}
you are using primitive boolean, so you need to initialize it to either true or false. You can use wrapper java.lang.Boolean instead.
To expand on that, Boolean is effectively tri-state: true, false or null. So you can put
Boolean input1 = null;
...
if (input1 == null) ...
对于局部变量,我们需要在使用之前对其进行初始化,而对于类/实例变量,则将它们隐式初始化为默认值(对于对象,则为null)
使用可以具有true / false / null的布尔类,或使用另一个变量来指示它们是否已初始化:您可以将它们初始化为false,添加另一个变量isInit并将其也初始化为false,一旦初始化变量,则切换isInit为真,这样您就可以检查它们是否使用isInit进行了初始化
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.