package homework;
import java.util.Scanner;
public class TargetSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个目标数字:");
int sum = 0;
if (sc.hasNextInt()) {
int target = sc.nextInt();
if (target > 0) {
for (int i = 1; i <= target; i++) {
sum += i;
}
System.out.println("\n从 1 累加至 " + target + " 的结果为:" + sum);
} else {
System.out.println("\n提示:输入的数字有误!");
}
} else {
System.out.println("\n提示:请按规则输入数字!");
}
sc.close();
}
}
Java目标数求和
最新推荐文章于 2023-12-23 17:35:21 发布