我有这个返回int:
的方法DataContext
我想返回测试,计算是否为blocs,每个都有自己的测试。问题是当我退出如果blocs和bloc 它说测试public Integer getMyTest()
{
Integer test;
for (int i = 0; i < this.id.getChoice().size(); i++)
{
if (this.id.getChoice().get(i).getCode().getId().equals(this.id.getId()))
{
if(this.value.equals(this.id.getChoice().get(i).getCode().getCode())
&& this.id.getChoice().get(i).getText().contains("Test1"))
{
Integer myresult = this.id.getTest().get(0);
test = myresult;
return test;
}
else if(this.value.equals(this.id.getChoice().get(i).getCode().getCode())
&& this.id.getChoice().get(i).getText().contains("Test2"))
{
Integer myresult = this.id.getTest().get(1);
test = myresult;
return test;
}
else if(this.value.equals(this.id.getChoice().get(i).getCode().getCode())
&& this.id.getChoice().get(i).getText().contains("Test3"))
{
Integer myresult = this.id.getTest().get(2);
test = myresult;
return test;
}
else if(this.value.equals(this.id.getChoice().get(i).getCode().getCode())
&& this.id.getChoice().get(i).getText().contains("Test4"))
{
Integer myresult = this.id.getTest().get(3);
test = myresult;
return test;
}
else if(this.value.equals(this.id.getChoice().get(i).getCode().getCode())
&& this.id.getChoice().get(i).getText().contains("Test5"))
{
Integer myresult = this.id.getTest().get(4);
test = myresult;
System.out.println("TEST 1 :" + test);
return test;
}
else if(this.value.equals(this.id.getChoice().get(i).getCode().getCode())
&& this.id.getChoice().get(i).getText().contains("Test5"))
{
Integer myresult = this.id.getTest().get(5);
test = myresult;
System.out.println("TEST 2 :" + test);
return test;
}
}
else return 0;
}
//here! is my problem ..
return test;
}
并且不保存/获得测试结果(其IF为真)。当我初始化为null时,它返回null值!
我应该怎么做才能解决这个问题并获得计算出来的测试?
感谢。
答案 0 :(得分:0)
您可以将代码更新为:
Integer test =0; // at the time of initialization
并替换
else return 0;
}
//here! is my problem ..
return test;
只是
else return test; // the last return within the scope of its declaration
另请注意,这是考虑您在更新值时返回所有其他条件的测试。
答案 1 :(得分:0)
你可以尝试的是通过说:Integer test = 0;
初始化测试
然后你可以随时返回测试,即使你return 0;
。这样,您的测试在循环开始之前初始化。
代码的问题在于,当它不应该传递给第一个时,测试永远不会被初始化,但是你会在for循环之后返回测试。
答案 2 :(得分:0)
据我所知,你的变量测试没用,因为每次为它赋值时,都会有一个return语句。所以我建议你删除变量测试及其所有赋值,然后返回myresult(或其他)。对于最后一次测试(返回未初始化测试的测试),返回在没有满足其他任何条件时应返回的任何值。如果它是一个错误的情况,你也可以用throw语句替换它。