import java.util.*;
public class App1{
public static void main(String[] args){
int n,i=1,sum=0;
Scanner buf=new Scanner(System.in);
do{
System.out.print("输入正整数:");
n=buf.nextInt();
}while(n<=0);
while(i<=n)
sum+=i++;
System.out.println("1+2+…+"+n+"="+sum);
}
}
运行结果 //例如输入45
输入正整数:45
1+2+…+45=1035
Process finished with exit code 0