Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + … + n.
Input
The input will consist of a series of integers n, one integer per line.
Output
For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.
代码
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int sum=0;
for(int i=1;i<=n;i++){
sum+=i;
}
System.out.println(sum);
System.out.println();
}
}
}
本文介绍了一个简单的编程挑战,即计算从1到n的所有整数之和,并提供了一个使用Java实现的具体示例。该任务来自HDOJ平台,旨在帮助初学者熟悉基本的循环和累积计算。
985

被折叠的 条评论
为什么被折叠?



