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.
Sample Input
1
100
Sample Output
1
5050
Author
DOOM III
简述一下哈,最近真是无聊,真是没事干,所以打算每天抽出几个小时来做做题,正直七夕哈,祝大家有情人终成眷属,表白的都成功,我打算敲一天代码,new一天对象,😄O(∩_∩)O哈哈~
当然这是最简单的一道题啦,所以我就不说思路咯,说一下用java A题的应该注意的事项:
1.必须要导包,千万不能忘记了
2.类名必须为Main
3.要注意输入、输出的格式
代码:
import java.util.Scanner;
public class Main1001 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int result = 0;
while (in.hasNextInt()) {
int a = in.nextInt();
for (int i = 1; i <= a; i++) {
result = result + i;
}
System.out.println(result);
System.out.println();
result = 0;
}
}
}