67,513
社区成员
发帖
与我相关
我的任务
分享
import javax.swing.JOptionPane;
public class InputCount {
public static void main(String[] args) {
String whole=JOptionPane.showInputDialog("请输入一个整数!");
int n=0;
try{
n=Integer.parseInt(whole);
if(n<1)
throw new NumberFormatException();
}catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "您输入的不是一个正整数!");
}
if(n==2){
System.out.println("1+2="+(1+2));
}
if(n==3){
System.out.println("1+2+3="+(1+2+3));
}else if(n>3){
int sum=0;
for(int i=1;i<=n;i++){
sum+=i;
}
System.out.println("1+2+3+...+"+n+"="+sum);
}
}
}