杭電oj-1001(Sum Problem)

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哈哈~

當(dāng)然這是最簡單的一道題啦,所以我就不說思路咯,說一下用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;
        }
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容