1 changed files with 35 additions and 0 deletions
@ -0,0 +1,35 @@ |
|||
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 x : sensorData) { |
|||
// 致命错误:传感器掉线,立即终止
|
|||
if (x == 999) { |
|||
System.out.println("致命错误:传感器掉线,终止处理"); |
|||
break; |
|||
} |
|||
|
|||
// 无效数据:越界,跳过
|
|||
if (x <= 0 || x > 100) { |
|||
System.out.println("警告:发现越界数据 [" + x + "],已跳过"); |
|||
continue; |
|||
} |
|||
|
|||
// 正常范围:计入统计
|
|||
validSum += x; |
|||
validCount++; |
|||
} |
|||
|
|||
// 最终输出
|
|||
if (validCount > 0) { |
|||
// 用双精度除法保留小数,避免整数除法陷阱
|
|||
double average = (double) validSum / validCount; |
|||
System.out.printf("有效数据平均值:%.2f%n", average); |
|||
} else { |
|||
System.out.println("无有效数据"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue