From a1c497e68e630050fd3a557d5ed2a9da847fa177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=95=85=E6=98=A5?= <3481369387@qq.com> Date: Thu, 19 Mar 2026 11:09:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4W2=E5=BC=82=E5=B8=B8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=B8=85=E6=B4=97=E4=BD=9C=E4=B8=9A=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Main.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Main.java diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..28d2ed2 --- /dev/null +++ b/Main.java @@ -0,0 +1,27 @@ +public class Main { + public static void main(String[] args) { + int[] voltages = {10, -5, 7, 105, 999, 89, 76, 74}; + int validCount = 0; // 有效数据个数 + double validSum = 0; // 有效数据总和 + + for (int voltage : voltages) { + if (voltage == 999) { + System.out.println("程序终止,传感器离线"); + break; // 遇到999,终止程序 + } else if (voltage < 0) { + System.out.println("警告:发现负数,数据已跳过"); + } else if (voltage >= 1 && voltage <= 100) { + validCount++; + validSum += voltage; + } + // 其他情况(如>100)不处理 + } + + if (validCount > 0) { + double average = validSum / validCount; + System.out.printf("有效数据个数:%d,平均值:%.2f\n", validCount, average); + } else { + System.out.println("没有收集到有效数据,打印初始状态"); + } + } +}