From 2427675ea3482d626d9190f145c9fce8fc140d2f Mon Sep 17 00:00:00 2001 From: pangyaxuan Date: Sun, 15 Mar 2026 23:22:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'W2'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- W2 | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 W2 diff --git a/W2 b/W2 new file mode 100644 index 0000000..5d1b069 --- /dev/null +++ b/W2 @@ -0,0 +1,29 @@ +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 data : sensorData) { + if (data == 999) { + System.out.println("致命错误:传感器掉线,终止处理"); + break; + } + if (data <= 0 || (data > 100 && data != 999)) { + System.out.println("警告:发现越界数据[" + data + "],已跳过"); + continue; + } + if (data >= 1 && data <= 100) { + validSum += data; + validCount++; + } + } + + if (validCount > 0) { + double average = (double) validSum / validCount; + System.out.println("有效数据平均值:" + average); + } else { + System.out.println("无有效数据"); + } + } +} \ No newline at end of file