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.
34 lines
1.3 KiB
34 lines
1.3 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 num : sensorData) {
|
|
// 规则3:致命错误,遇到999直接终止流程
|
|
if (num == 999) {
|
|
System.out.println("致命错误:传感器掉线,终止处理");
|
|
break;
|
|
}
|
|
// 规则2:无效数据(0/负数/大于100且不是999),跳过并告警
|
|
if (num <= 0 || num > 100) {
|
|
System.out.println("警告:发现越界数据[" + num + "],已跳过");
|
|
continue;
|
|
}
|
|
// 规则1:正常范围(1-100),计入总和与计数
|
|
validSum += num;
|
|
validCount++;
|
|
}
|
|
|
|
// 规则4:最终输出平均值
|
|
if (validCount > 0) {
|
|
// 避免整数除法陷阱:将总和转为double再计算
|
|
double average = (double) validSum / validCount;
|
|
System.out.printf("有效数据的平均值为:%.2f%n", average);
|
|
} else {
|
|
System.out.println("无有效数据");
|
|
}
|
|
}
|
|
}
|