编写一段程序,读入正数N,然后计算并显示前N个奇数整数的和。
如题,不知如何下手
程序代码:import java.util.Scanner;
public class OddSum {
public static void main(String[] args) {
int odd = 1, sum = 0, n;
Scanner reader = new Scanner(System.in);
n = reader.nextInt();
for(int i = 0; i < n; i++, odd += 2)
sum += odd;
System.out.printf("%d\n", sum);
}
} /* Output
4
16
*/
