我有一个非常简单的问题,但我不知道是什么。示例代码如下
public class test {
public static void main(String[] args) {
test test = new test();
int temp = test.method(0);
System.out.println("temp = " + temp);
}
public int method(int i) {
if (i < 7) {
i++;
method(i);
}
return i;
}
}
如果我想在主临时工中获得 7 我该怎么办?只使用其他静态参数?或者有什么办法可以实现这一目标吗?
最佳答案
public int method(int i) {
if (i < 7) {
i++;
return method(i);
} else {
return i;
}
}
应该可以正常工作:)
分割:
IF i is smaller than 7, increment i and return method(i)
ELSE return i (must be 7)
else block 是可选的:
public int method(int i) {
if (i < 7) {
i++;
return method(i);
}
return i;
}
但是如果你有 else 的话会更清楚,因为它是有条件的。
关于java - java中如何控制方法中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28468007/