1 changed files with 46 additions and 0 deletions
@ -0,0 +1,46 @@ |
|||
//TIP 要<b>运行</b>代码,请按 <shortcut actionId="Run"/> 或
|
|||
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
|
|||
void main() { |
|||
//TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
|
|||
// 查看 IntelliJ IDEA 建议如何修正。
|
|||
IO.println(String.format("Hello and welcome!")); |
|||
for (int i = 1; i <= 5; i++) { |
|||
//TIP 按 <shortcut actionId="Debug"/> 开始调试代码。我们已经设置了一个 <icon src="AllIcons.Debugger.Db_set_breakpoint"/> 断点
|
|||
// 但您始终可以通过按 <shortcut actionId="ToggleLineBreakpoint"/> 添加更多断点。
|
|||
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 num : sensorData) { |
|||
if (num == 999) { |
|||
// 致命错误:终止流程
|
|||
System.out.println("致命错误:传感器断线,终止处理"); |
|||
break; |
|||
} else if (num <= 0 || num > 100) { |
|||
// 无效数据:跳过并打印警告
|
|||
System.out.println("警告:无效数据 [" + num + "],已跳过"); |
|||
continue; |
|||
} else { |
|||
// 正常数据:计入总和与个数
|
|||
validSum += num; |
|||
validCount++; |
|||
} |
|||
} |
|||
|
|||
// 计算并输出结果
|
|||
if (validCount > 0) { |
|||
double average = (double) validSum / validCount; |
|||
System.out.println("有效数据的平均值为:" + average); |
|||
} else { |
|||
System.out.println("无有效数据"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
Loading…
Reference in new issue