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.

36 lines
1.2 KiB

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) {
// 1. 致命错误:999
if (data == 999) {
System.out.println("致命错误: 传感器掉线, 终止处理");
break;
}
// 2. 正常数据:1~100
else if (data >= 1 && data <= 100) {
validSum += data;
validCount++;
}
// 3. 无效数据:跳过
else {
System.out.println("警告: 发现越界数据[" + data + "], 已跳过");
continue;
}
}
// 输出结果
if (validCount > 0) {
double avg = (double) validSum / validCount;
System.out.println("有效数据平均值: " + avg);
} else {
System.out.println("无有效数据");
}
}
}