编写一段程序,读入正数N,然后计算并显示前N个奇数整数的和。
如题,不知如何下手
如题,不知如何下手
2011-06-23 21:15
2011-06-23 23:14
2011-06-24 11:59
2011-06-24 13:38
2011-06-24 13:40
2011-06-24 14:02
2011-06-24 17:36
程序代码: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
*/

2011-06-25 12:44