0
试图设计一个读取整数并打印2和输入值之间的所有偶数和的应用程序。任何人都可以帮助我最后一点?!Usind for while while循环更好
import java.util.Scanner;
public class IntegerValue {
// main method
public static void main(String[] args) {
// Data fields
int a;
// end
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer greater than 1");
a = sc.nextInt();
if (a <= 1) {
System.out.println("Input Value must not be less than 2");
}
while (a > 1) {
int sum = 0;
if (a % 2 == 0) {
sum += a;
a = a - 1;
}
System.out.println(sum);
}
}
}
为什么不使用计算前n个偶数之和是'N *(N + 1)'更简单,更快的公式? –