我有3节课。我试图通过名为“ main”的对象在main方法中设置一个变量,并通过编写super.getnumberofsets()来重用该值;在for循环中,但显示“缺少2个分支中的1个”。
public static void main(){
//in the main method
//Main class maintain the private variables with their setters and getters.
Main main = new Main();
Sets sets = new Sets();
System.out.print("Enter how many sets you want to create: ");
newnumberofsets = in.nextInt();
main.set_numberofsets(newnumberofsets);
sets.setgroups();
sets.getgroups();
}
// in Sets class
protected void setgroups()
{
//In this loop it says "1 of 2 branches missed".
for(int x = 0; x<super.getnumberofsets();x++) {
main_zeroarray[x] = new Main0();
}
}
protected void getgroups() {
count = 1;
for(int x = 0 ;x < super.getnumberofsets();x++) {
System.out.println(count + ". Set " + setnames[x]);
count++;
}
}
我希望super关键字将从对象main.set_newnumberofsets(newnumberofsets);读取相同的值。
答案 0 :(得分:0)
最有可能的解释是,super.getnumberofsets()
返回0
,因此循环终止条件x < super.getnumberofsets()
为false
,因此,循环增量x++
从未执行,因此成为“遗漏的分支”。