Week 1-8: Spring Boot 学习计划完整项目
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
118
week1/day04/StudentScoreManager.java
Normal file
118
week1/day04/StudentScoreManager.java
Normal file
@@ -0,0 +1,118 @@
|
||||
package day04;
|
||||
|
||||
/**
|
||||
* 第 4 天:学生成绩管理器 —— 数组、ArrayList、方法
|
||||
* 目标:掌握数据组织(数组/动态数组)和代码组织(方法封装)
|
||||
*
|
||||
* 功能:添加成绩、查看全部、求平均分、找最高/最低分
|
||||
*/
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class StudentScoreManager {
|
||||
|
||||
// Scanner 作为静态成员,全局复用(记得最后 close)
|
||||
static Scanner scanner = new Scanner(System.in);
|
||||
// ArrayList<Integer> 动态存储成绩
|
||||
static ArrayList<Integer> scores = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
while (true) {
|
||||
printMenu();
|
||||
System.out.print("请选择操作 (1-5): ");
|
||||
int choice = scanner.nextInt();
|
||||
|
||||
switch (choice) {
|
||||
case 1 -> addScore();
|
||||
case 2 -> showAllScores();
|
||||
case 3 -> showAverage();
|
||||
case 4 -> showMaxMin();
|
||||
case 5 -> {
|
||||
System.out.println("再见!");
|
||||
scanner.close();
|
||||
return; // 退出程序
|
||||
}
|
||||
default -> System.out.println("无效选项,请重试。");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 方法定义 ====================
|
||||
|
||||
/** 打印菜单 */
|
||||
static void printMenu() {
|
||||
System.out.println("\n========== 学生成绩管理器 ==========");
|
||||
System.out.println("1. 添加成绩");
|
||||
System.out.println("2. 查看全部成绩");
|
||||
System.out.println("3. 查看平均分");
|
||||
System.out.println("4. 查看最高分 / 最低分");
|
||||
System.out.println("5. 退出");
|
||||
System.out.println("====================================");
|
||||
}
|
||||
|
||||
/** 添加成绩 */
|
||||
static void addScore() {
|
||||
System.out.print("请输入成绩 (0-100): ");
|
||||
int score = scanner.nextInt();
|
||||
|
||||
// 基本输入校验
|
||||
if (score < 0 || score > 100) {
|
||||
System.out.println("成绩必须在 0-100 之间!");
|
||||
return;
|
||||
}
|
||||
|
||||
scores.add(score);
|
||||
System.out.println("✅ 第 " + scores.size() + " 个学生的成绩已添加: " + score);
|
||||
}
|
||||
|
||||
/** 查看全部成绩 */
|
||||
static void showAllScores() {
|
||||
if (scores.isEmpty()) {
|
||||
System.out.println("暂无成绩记录。");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("\n--- 全部成绩 ---");
|
||||
// 传统 for 循环(需要索引)
|
||||
for (int i = 0; i < scores.size(); i++) {
|
||||
System.out.printf("学生 %d: %d 分\n", i + 1, scores.get(i));
|
||||
}
|
||||
System.out.println("共 " + scores.size() + " 名学生的成绩。");
|
||||
}
|
||||
|
||||
/** 求平均分 */
|
||||
static void showAverage() {
|
||||
if (scores.isEmpty()) {
|
||||
System.out.println("暂无成绩记录,无法计算平均分。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 遍历求和
|
||||
int sum = 0;
|
||||
for (int s : scores) { // 增强 for 循环(foreach)
|
||||
sum += s;
|
||||
}
|
||||
double average = (double) sum / scores.size(); // 强制转换防截断
|
||||
|
||||
System.out.printf("平均分: %.2f(共 %d 名学生)\n", average, scores.size());
|
||||
}
|
||||
|
||||
/** 找最高分和最低分 */
|
||||
static void showMaxMin() {
|
||||
if (scores.isEmpty()) {
|
||||
System.out.println("暂无成绩记录。");
|
||||
return;
|
||||
}
|
||||
|
||||
int max = scores.get(0);
|
||||
int min = scores.get(0);
|
||||
|
||||
for (int s : scores) {
|
||||
if (s > max) max = s;
|
||||
if (s < min) min = s;
|
||||
}
|
||||
|
||||
System.out.println("最高分: " + max);
|
||||
System.out.println("最低分: " + min);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user