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.

32 lines
1.1 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 value : sensorData) {
if (value == 999) {
System.out.println("致命错误:传感器掉线,终止处理");
break;
}
if (value <= 0 || (value > 100 && value != 999)) {
System.out.println("警告:发现越界数据 [" + value + "],已跳过");
continue;
}
// 1~100 之间的有效数据
validSum += value;
validCount++;
}
if (validCount > 0) {
double avg = (double) validSum / validCount; // 强转 double,避免整数除法
System.out.println("有效数据平均值:" + avg);
} else {
System.out.println("无有效数据");
}
}
}