From 9905948d0342a790e933ef7dc13fc6aa5b5b16be Mon Sep 17 00:00:00 2001 From: YuWeixia Date: Sun, 15 Mar 2026 21:16:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'w2/=E4=BB=A3=E7=A0=81'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w2/代码 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 w2/代码 diff --git a/w2/代码 b/w2/代码 new file mode 100644 index 0000000..b7897cc --- /dev/null +++ b/w2/代码 @@ -0,0 +1,32 @@ +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 value : sensorData) { + if (value == 999) { + System.out.println("致命错误:传感器掉线,终止处理"); + break; + } + + if (value <= 0 || value > 100) { + System.out.println("警告:发现越界数据 " + value + ",已跳过"); + continue; + } + + // 有效数据(1到100之间) + validSum += value; + validCount++; + } + + // 输出结果 + if (validCount > 0) { + double average = (double) validSum / validCount; + System.out.println("有效数据平均值为:" + average); + } else { + System.out.println("无有效数据"); + } + } +} \ No newline at end of file