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.

33 lines
1021 B

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 value : sensorData) {
if (value == 999) {
System.out.println("致命错误:传感器掉线,终止处理");
break;
}
// 无效数据:0、负数、大于100(但不包括999,因为999已经单独判断)
if (value <= 0 || value > 100) {
continue;
}
// 正常范围:1~100
validSum += value;
validCount++;
}
// 输出平均值
if (validCount > 0) {
double average = (double) validSum / validCount;
System.out.println("有效数据平均值: " + average);
} else {
System.out.println("没有有效数据");
}
}
}