它说java中遗漏了2个分支中的1个
我有三节课。我试图通过一个名为“main”的对象在main方法中设置一个变量,并通过编写super.getnumberofsets()重用该值;在for循环中,但它表示“2个分支中有1个遗漏”它说java中遗漏了2个分支中的1个,java,eclipse,Java,Eclipse,我有三节课。我试图通过一个名为“main”的对象在main方法中设置一个变量,并通过编写super.getnumberofsets()重用该值;在for循环中,但它表示“2个分支中有1个遗漏” publicstaticvoidmain(){ //主要方法 //Main类使用其setter和getter维护私有变量。 Main Main=新Main(); 集合=新集合(); System.out.print(“输入要创建的集的数量:”); newnumberofsets=in.nextInt();
publicstaticvoidmain(){
//主要方法
//Main类使用其setter和getter维护私有变量。
Main Main=新Main();
集合=新集合();
System.out.print(“输入要创建的集的数量:”);
newnumberofsets=in.nextInt();
main.set\u numberofsets(newnumberofsets);
sets.setgroups();
sets.getgroups();
}
//成套课
受保护的void集合组()
{
//在这个循环中,它表示“2个分支中有1个遗漏”。
对于(int x=0;xlopp中存在遗漏分支的最可能解释是super.getnumberofsets()
返回0
,因此循环终止条件x
为false
,因此循环增量x++
永远不会执行,使其成为“遗漏的分支”.运行代码时是否出现异常
?Eclipse编译代码时是否出现编译错误?@Abra-没有编译错误,但当我指定值(例如5)时,它应该打印5条语句,但它不打印,我会添加其他函数以提供完整信息。然后您可以-阅读代码。当我赋值(例如5)时,它应该打印5条语句。您是否在问题中出现的代码中引用了方法getgroups()
中的循环?是的,getgroups();很明显,从您发布的代码中,super.getnumberofsets()
没有返回您期望的值。不幸的是,您没有发布一个,因此我建议您尝试调试代码,以便查看值在何时何地更改。
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++;
}
}