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.5 KiB

public class DataCleaner {
public static void main(String[] args) {
// 1. 定义传感器采集的原始数据数组
int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76};
// 2. 初始化有效数据总和与有效数据个数
int validSum = 0; // 存储有效数据的总和
int validCount = 0; // 统计有效数据的数量
// 3. 遍历数组中的每一个数据
for (int data : sensorData) {
// 3.1 检查是否为致命错误(传感器掉线)
if (data == 999) {
System.out.println("致命错误:传感器掉线,终止处理");
break; // 终止整个循环,不再处理后续数据
}
// 3.2 检查是否为无效数据(越界)
if (data <= 0 || data > 100) {
System.out.println("警告:发现越界数据 [" + data + "],已跳过");
continue; // 跳过当前数据,继续处理下一个数据
}
// 3.3 有效数据,计入总和与计数
validSum += data;
validCount++;
}
// 4. 计算并输出最终结果
if (validCount > 0) {
// 将总和转为double类型,避免整数除法导致的精度丢失
double average = (double) validSum / validCount;
System.out.println("有效数据平均值:" + average);
} else {
System.out.println("无有效数据");
}
}
}