Java从键盘上输入一个正整数n,然后计算1+2+...+n的结果并输出
输入:
-6
10
package com;
import java.util.*;
public class app4_8 {
public static void main(String[] args)
{
int n,i=1,sum=0;
Scanner buf=new Scanner (System.in);
do{
System.out.println("输入正整数:");
n=buf.nextInt();
}
while(n<0);
while(i<=n)
sum+=i++;
System.out.println("1+2+...+"+n+"="+sum);
}
}