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.
45 lines
1.5 KiB
45 lines
1.5 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 i = 0; i < sensorData.length; i++) {
|
|
int currentData = sensorData[i];
|
|
|
|
// 检查是否遇到致命错误(999)
|
|
if (currentData == 999) {
|
|
System.out.println("致命错误:传感器掉线,终止处理");
|
|
break;
|
|
}
|
|
|
|
// 检查数据是否在有效范围(1-100)
|
|
if (currentData >= 1 && currentData <= 100) {
|
|
validSum += currentData;
|
|
validCount++;
|
|
} else {
|
|
// 无效数据,打印警告并跳过
|
|
System.out.println("警告:发现越界数据 " + currentData + ",已跳过");
|
|
continue;
|
|
}
|
|
}
|
|
|
|
// 计算并输出结果
|
|
System.out.println("\n=== 处理结果 ===");
|
|
if (validCount > 0) {
|
|
// 注意:要转换为double避免整数除法
|
|
double average = (double) validSum / validCount;
|
|
System.out.println("有效数据个数:" + validCount);
|
|
System.out.println("有效数据总和:" + validSum);
|
|
System.out.println("有效数据平均值:" + average);
|
|
} else {
|
|
System.out.println("无有效数据");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|