我在打印偶数时遇到问题。如果我将参数设置为 1,7,它会打印出 (2,4,6) 之间的偶数,这正是我想要的。当 int i 大于 int n 时,我希望它以递减的方式打印出来,但 if 语句根本不执行。我不知道是什么问题???
public class Problem {
public static void main(String[] args) {
printEven(1,7);
printEven(21, 15);
//main
}
public static void printEven(int i, int n) {
System.out.print("[ ");
//if 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
}
//if i is greater than n
if (i >= n) {
for (int t = n; t >= i; t--) {
if (t % 2 == 0) {
System.out.print(t + " ");
//if statement inner
}
//for loop
}
//if statement outer
}
System.out.print("]");
System.out.println();
//printEven
}
//class
}