2 changed files with 32 additions and 32 deletions
Binary file not shown.
@ -1,33 +1,33 @@ |
|||||
public class DataCleaner { |
public class DataCleaner { |
||||
public static void main(String[] args) { |
public static void main(String[] args) { |
||||
int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76}; |
int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76}; |
||||
|
|
||||
int validSum = 0; // 有效数据总和
|
int validSum = 0; // 有效数据总和
|
||||
int validCount = 0; // 有效数据个数
|
int validCount = 0; // 有效数据个数
|
||||
|
|
||||
// 遍历数组
|
// 遍历数组
|
||||
for (int value : sensorData) { |
for (int value : sensorData) { |
||||
if (value == 999) { |
if (value == 999) { |
||||
System.out.println("致命错误:传感器掉线,终止处理"); |
System.out.println("致命错误:传感器掉线,终止处理"); |
||||
break; |
break; |
||||
} |
} |
||||
|
|
||||
if (value <= 0 || value > 100) { |
if (value <= 0 || value > 100) { |
||||
System.out.println("警告:发现越界数据 " + value + ",已跳过"); |
System.out.println("警告:发现越界数据 " + value + ",已跳过"); |
||||
continue; |
continue; |
||||
} |
} |
||||
|
|
||||
// 有效数据(1到100之间)
|
// 有效数据(1到100之间)
|
||||
validSum += value; |
validSum += value; |
||||
validCount++; |
validCount++; |
||||
} |
} |
||||
|
|
||||
// 输出结果
|
// 输出结果
|
||||
if (validCount > 0) { |
if (validCount > 0) { |
||||
double average = (double) validSum / validCount; |
double average = (double) validSum / validCount; |
||||
System.out.println("有效数据平均值为:" + average); |
System.out.println("有效数据平均值为:" + average); |
||||
} else { |
} else { |
||||
System.out.println("无有效数据"); |
System.out.println("无有效数据"); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
Loading…
Reference in new issue