From 6fa3630a7fab7918792d09e2ebff1fff1f61426a Mon Sep 17 00:00:00 2001 From: Jiayuheng Date: Tue, 17 Mar 2026 16:44:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w2'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w2/DataCleaner.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 w2/DataCleaner.java diff --git a/w2/DataCleaner.java b/w2/DataCleaner.java new file mode 100644 index 0000000..ac8cf0f --- /dev/null +++ b/w2/DataCleaner.java @@ -0,0 +1,36 @@ +//TIP 要运行代码,请按 或 +// 点击装订区域中的 图标。 +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 x : sensorData) { + // 3. 致命错误:遇到999,终止处理 + if (x == 999) { + System.out.println("致命错误:传感器掉线,终止处理"); + break; + } + // 2. 无效数据:0/负数/大于100(非999),跳过并告警 + if (x <= 0 || x > 100) { + System.out.println("警告:发现越界数据[" + x + "],已跳过"); + continue; + } + // 1. 正常范围:1~100,计入有效数据 + validSum += x; + validCount++; + } + + // 4. 最终输出:计算平均值或提示无有效数据 + if (validCount > 0) { + // 避免整数除法陷阱,转换为double计算 + double average = (double) validSum / validCount; + System.out.println("有效数据平均值:" + average); + } else { + System.out.println("无有效数据"); + } + } +} \ No newline at end of file