我有以下Java代码。根据返回值递增值
public class Test {
public static void main(String[] args) {
int i = 5;
int j = 0;
for (int k = 1; k <= i; k++) {
System.out.println("row count is " + j);
increment(j);
j += 1;
}
}
private static int increment(int j) {
if (j == 2) {
j += 1;
System.out.println("row count is " + j);
}
return j;
}
}
这里我想根据返回值增加j
的值。
我得到的当前输出是。
row count is 0
row count is 1
row count is 2
row count is 3
row count is 3
row count is 4
我的预期输出是
row count is 0
row count is 1
row count is 2
row count is 3
row count is 4
row count is 5
这里我知道,把
if (j == 2) {
j += 1;
System.out.println("row count is " + j);
}
在我for
块解决了这个问题,但是这是一个像我主要的翻版代码以我输入的形式提供。我必须遵循这种模式,我的意思是通过检查我的方法中的条件来增加值。
请让我知道如何得到这个。
谢谢
你确实需要increment'的'的返回值赋给'j' – SomeJavaGuy