0
我似乎無法弄清楚這裏有什麼問題。在第三種情況下,我只希望它打印出16次(如主要方法中給出的那樣)。但由於一些奇怪的原因,它將其打印出來3次。有人可以解釋爲什麼?如果陳述和印刷太多
public class Foursix {
public static void main(String[] args) {
printEven(1,7);
printEven(21,2);
printEven(16,16);
//main
}
public static void printEven(int i, int n) {
System.out.print("[ ");
//n is greater than i
if (i <= n) {
for (int t = i; t <= n; t++) {
if (t % 2 == 0) {
System.out.print(t + " ");
//if statement inner
}
//for loop
}
//if statement outer
}
//i is greater than n
if (i >= n) {
for (int t = i; t >= n; t--) {
if (t % 2 == 0) {
System.out.print(t + " ");
//if statement inner
}
//for loop
}
//if statement outer
}
//i is the same as n
if (i == n && i % 2 == 0) {
System.out.print(i);
//if statement
}
System.out.print("]");
System.out.println();
//printEven
}
//class
}
保持語言乾淨...... –
也接受答案 – tom