从键盘中任意输入一个整数,用for循环算出该整数从1到整数的和:
实例代码如下:
Scanner sc=new Scanner(System.in);
System.out.println("输入一个整数:");
int a=sc.nextInt();
int b=0;
for (int i = 0; i <=a; i++) {
// b+=i;//也可写成:
b=i+b;
} System.out.println(b);