我有这两个方法,Java无法在getNumEmails()中找到“返回”。所有这些都在同一个班,其中只有静态方法ifs混淆Java内部返回
private static int posSymbol=0;
private static int cont=0;
private static String text=null;
private static int getPosSymbol(){
posSymbol=text.indexOf('@',posSymbol);//if there is no symbol, returns -1
return posSymbol;
}
//Main calls this method
public static int getNumEmails(String completeText){
text=completeText;
while(posSymbol!=(-1)){
posSymbol=getPosSymbol();
if(posSymbol!=(-1)){
cont++;
posSymbol++;
}//close if
else{
return cont; //It seems that it doesn't reach the return always
}//close else
}//close while
}//close method
我知道,解决方法很简单,删除了“其他”,并把返回续;过了一段时间。但是我想知道为什么Java认为getNumEmails()可以在不返回任何东西的情况下结束。
这肯定会退出而不返回如果getPosSymbol的值()为-2或从来没有-1 ... – Kendrick
是什么使你认为它会始终返回的东西吗? 'else'子句可能永远不会被执行。 –
@Kendrick。是真的,但编译器不会进入该级别的考试。它只是看到一个'else'。 –