From 85d656aa80c677a7586c51d547a687335512aecf Mon Sep 17 00:00:00 2001 From: ZhengJiayin <13230092115@163.com> Date: Mon, 23 Mar 2026 18:39:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(w2):=20=E5=AE=8C=E6=88=90=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=B8=85=E6=B4=97=E4=BD=9C=E4=B8=9A=EF=BC=88=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=B5=8C=E5=A5=97=E4=BB=93=E5=BA=93=EF=BC=8C=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E5=AE=8C=E6=95=B4=E4=BB=A3=E7=A0=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w2 | 1 - w2/DataCleaner.java | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) delete mode 160000 w2 create mode 100644 w2/DataCleaner.java diff --git a/w2 b/w2 deleted file mode 160000 index fd45640..0000000 --- a/w2 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit fd456402449e69bf3e19b4065596e40001045371 diff --git a/w2/DataCleaner.java b/w2/DataCleaner.java new file mode 100644 index 0000000..f0d23d6 --- /dev/null +++ b/w2/DataCleaner.java @@ -0,0 +1,34 @@ +public class DataCleaner { + public static void main(String[] args) { + int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76}; + + int validSum = 0; // 有效数据总和 + int validCount = 0; // 有效数据个数 + + // 遍历数组,按规则处理数据 + for (int num : sensorData) { + // 规则3:致命错误,遇到999直接终止流程 + if (num == 999) { + System.out.println("致命错误:传感器掉线,终止处理"); + break; + } + // 规则2:无效数据(0/负数/大于100且不是999),跳过并告警 + if (num <= 0 || num > 100) { + System.out.println("警告:发现越界数据[" + num + "],已跳过"); + continue; + } + // 规则1:正常范围(1-100),计入总和与计数 + validSum += num; + validCount++; + } + + // 规则4:最终输出平均值 + if (validCount > 0) { + // 避免整数除法陷阱:将总和转为double再计算 + double average = (double) validSum / validCount; + System.out.printf("有效数据的平均值为:%.2f%n", average); + } else { + System.out.println("无有效数据"); + } + } +} \ No newline at end of file