根據偏高/偏低天數,給出簡單的健康建議,讓使用者不只是看到數據,還知道怎麼改善。
新增程式碼解釋:
1.把原本的 printStatusSummary() 改成 printStatusSummaryAndAdvice()。
先統計偏高、偏低、正常天數,接著判斷哪一個最多並給出適當的建議。
public static void printStatusSummaryAndAdvice() {
int highCount = 0;
int lowCount = 0;
int normalCount = 0;
for (String status : statusArray) {
if (status.equals("偏高")) {
highCount++;
} else if (status.equals("偏低")) {
lowCount++;
} else {
normalCount++;
}
}
2.統計目前為止的狀態
System.out.println("\n=== 狀態統計 ===");
System.out.println("偏高天數:" + highCount);
System.out.println("偏低天數:" + lowCount);
System.out.println("正常天數:" + normalCount);
3.根據統計後的天數給出建議
System.out.println("\n=== 健康建議 ===");
if (highCount > lowCount && highCount > normalCount) {
System.out.println("偏高天數較多,建議少鹽少油,保持運動!");
} else if (lowCount > highCount && lowCount > normalCount) {
System.out.println("偏低天數較多,建議注意休息,多補充水分!");
} else {
System.out.println("血壓狀態大致正常,請保持良好生活作息!");
}
4.放在所有輸出最後,讓使用者可以先看數據再看建議
printStatusSummaryAndAdvice();
執行結果:
=== 第 1 天輸入 ===
請輸入收縮壓: 99
請輸入舒張壓: 60
請輸入脈搏: 104
今日狀態:正常
=== 第 2 天輸入 ===
請輸入收縮壓: 100
請輸入舒張壓: 63
請輸入脈搏: 91
今日狀態:正常
=== 第 3 天輸入 ===
請輸入收縮壓: 100
請輸入舒張壓: 58
請輸入脈搏: 122
今日狀態:偏低
=== 目前所有紀錄 ===
第 1 天:99/60 mmHg, 脈搏:104 → 狀態:正常
第 2 天:100/63 mmHg, 脈搏:91 → 狀態:正常
第 3 天:100/58 mmHg, 脈搏:122 → 狀態:偏低
=== 平均值 ===
平均收縮壓:99.66666666666667
平均舒張壓:60.333333333333336
平均脈搏:105.66666666666667
=== 趨勢分析 ===
整體狀態:正常,請保持!
=== 最高 / 最低血壓日 & 變異幅度 ===
最高血壓:第 2 天 → 100/63
最低血壓:第 1 天 → 99/60
收縮壓變異幅度:1 mmHg
血壓波動穩定
=== 狀態統計 ===
偏高天數:0
偏低天數:1
正常天數:2
=== 健康建議 ===
血壓狀態大致正常,請保持良好生活作息!