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題的應(yīng)該注意的事項:
1.必須要導(dǎo)包,千萬不能忘記了
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;
}
}
}