今日问题:
1.return 返回值的问题
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.add(5, 8);
}
public static int add(int a, int b){
for (int i = 0; i < 5 ; i++) {
if((i+a)%2==0 & (i+b)%2==0){
int res = a+b;
System.out.println(res);
return res;
break;
}else {
int res = a-b;
System.out.println(res);
return res;
}
}
}
}
出现的问题,
1.没有返回值?为什么(明明if------else里面都有返回值,缺提示没有返回值)
猜测:可能是因为java程序编译的时候只识别是for循环,而不会识别for循环里面的条件(即它不知道你程序能不能满足条件进入for循环)所以,人会可能会没有返回值,提示返回值无效
2.无法访问的语句break;
return ,返回值之后,已经跳出方法,break无法被访问。