You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
1.0 KiB
27 lines
1.0 KiB
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("没有收集到有效数据,打印初始状态");
|
|
}
|
|
}
|
|
}
|
|
|