.编写一个JAVA小程序,接受用户从键盘输入的一个正整数,然后统计并输出从1到这个正整数的累加和

2024-12-26 14:03:13
推荐回答(3个)
回答1:

import java.util.Scanner;
public class Test
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int count = 0;
int num = scan.nextInt();
for(int i = 1;i<=num;i++){
count +=i;
}
System.out.println(count);
}
}

回答2:

import java.util.Scanner;
public class Test
{
public static void main(String args[])
{
Scanner scan = null ;
int num = 0;
try{
scan = new Scanner(System.in);
num = scan.nextInt();
}catch(Exception e){
System.out.println("Exception: " +e.getMessage());
}
int count = 0;
for(int i = 1;i<=num;i++){
count += i;
}
System.out.println(count);
}
}

回答3:

控制台的?