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.
37 lines
1.4 KiB
37 lines
1.4 KiB
public class Main {
|
|
public static void main(String[] args) {
|
|
int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76};
|
|
|
|
int validSum = 0; // 有效数据总和
|
|
int validCount = 0; // 有效数据个数
|
|
|
|
// 遍历传感器数据数组
|
|
for (int i = 0; i < sensorData.length; i++) {
|
|
int value = sensorData[i];
|
|
|
|
// 检查是否为致命错误(传感器断开连接)
|
|
if (value == 999) {
|
|
System.out.println("致命错误:传感器掉线,终止处理");
|
|
break; // 立即终止整个数据处理流程
|
|
}
|
|
|
|
// 检查是否为无效数据(0或负数,或大于100)
|
|
if (value <= 0 || (value > 100 && value != 999)) {
|
|
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("无有效数据");
|
|
}
|
|
}
|
|
}
|