Submit Time:2026-02-01 10:17:44

运行 ID: 377499

import java.util.Scanner; // 强制主类名Main,在线测评必备 public class Main { public static void main(String[] args) { // 创建Scanner对象读取输入 Scanner sc = new Scanner(System.in); // 核心:hasNextInt()检测是否有下一个整数,EOF时自动退出循环 while (sc.hasNextInt()) { int n = sc.nextInt(); // 读取每行的整数n int sum = n * (n + 1) / 2; // 等差数列求和公式,无循环更高效 System.out.println(sum); // 每行对应输出一个结果 } sc.close(); // 关闭流,不影响测评,养成习惯 } }