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.
30 lines
918 B
30 lines
918 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 data : sensorData) {
|
|
if (data == 999) {
|
|
System.out.println("致命错误:传感器掉线,终止处理");
|
|
break;
|
|
}
|
|
|
|
if (data <= 0 || data > 100) {
|
|
System.out.printf("警告:发现越界数据 %d,已跳过%n", data);
|
|
continue;
|
|
}
|
|
|
|
validSum += data;
|
|
validCount += 1;
|
|
}
|
|
|
|
if (validCount == 0) {
|
|
System.out.println("无有效数据");
|
|
} else {
|
|
float average = (float) validSum / (float) validCount;
|
|
System.out.printf("数据平均值:%s", average);
|
|
}
|
|
}
|
|
}
|