6 changed files with 36 additions and 66 deletions
Binary file not shown.
Binary file not shown.
@ -0,0 +1,36 @@ |
|||
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) { |
|||
// 致命错误:遇到 999 立即终止流程
|
|||
if (data == 999) { |
|||
System.out.println("致命错误: 传感器掉线, 终止处理"); |
|||
break; |
|||
} |
|||
|
|||
// 无效数据:0、负数 或 大于100且不是999
|
|||
if (data <= 0 || (data > 100 && data != 999)) { |
|||
System.out.println("警告: 发现越界数据[" + data + "], 已跳过"); |
|||
continue; |
|||
} |
|||
|
|||
// 正常范围:1~100 之间,计入有效数据
|
|||
validSum += data; |
|||
validCount++; |
|||
} |
|||
|
|||
// 最终输出结果
|
|||
if (validCount > 0) { |
|||
// 注意:用浮点除法避免整数除法陷阱
|
|||
double average = (double) validSum / validCount; |
|||
System.out.println("有效数据平均值: " + average); |
|||
} else { |
|||
System.out.println("无有效数据"); |
|||
} |
|||
} |
|||
} |
|||
@ -1,66 +0,0 @@ |
|||
import java.util.Scanner; |
|||
|
|||
/** |
|||
* 温度转换器(Java版) |
|||
* 支持摄氏度(°C)与华氏度(°F)之间互转 |
|||
*/ |
|||
public class TemperatureConverter { |
|||
|
|||
/** |
|||
* 将摄氏度转换为华氏度 |
|||
* @param c 摄氏温度 |
|||
* @return 对应的华氏温度 |
|||
*/ |
|||
public static double celsiusToFahrenheit(double c) { |
|||
return c * 9.0 / 5.0 + 32.0; |
|||
} |
|||
|
|||
/** |
|||
* 将华氏度转换为摄氏度 |
|||
* @param f 华氏温度 |
|||
* @return 对应的摄氏温度 |
|||
*/ |
|||
public static double fahrenheitToCelsius(double f) { |
|||
return (f - 32.0) * 5.0 / 9.0; |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
Scanner scanner = new Scanner(System.in); |
|||
|
|||
// 提示用户输入
|
|||
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
|||
String input = scanner.nextLine().trim(); |
|||
|
|||
// 空输入判断
|
|||
if (input.isEmpty()) { |
|||
System.out.println("输入为空,程序退出。"); |
|||
scanner.close(); |
|||
return; |
|||
} |
|||
|
|||
// 分割输入内容
|
|||
String[] parts = input.split("\\s+"); // 按任意空格分割
|
|||
|
|||
try { |
|||
// 解析数值和单位
|
|||
double value = Double.parseDouble(parts[0]); |
|||
String unit = parts.length > 1 ? parts[1].toUpperCase() : "C"; |
|||
|
|||
// 执行转换
|
|||
if (unit.startsWith("C")) { |
|||
double fahrenheit = celsiusToFahrenheit(value); |
|||
System.out.printf("%.2f °C = %.2f °F%n", value, fahrenheit); |
|||
} else if (unit.startsWith("F")) { |
|||
double celsius = fahrenheitToCelsius(value); |
|||
System.out.printf("%.2f °F = %.2f °C%n", value, celsius); |
|||
} else { |
|||
System.out.println("未知单位,请使用 C 或 F。"); |
|||
} |
|||
|
|||
} catch (Exception e) { |
|||
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); |
|||
} finally { |
|||
scanner.close(); |
|||
} |
|||
} |
|||
} |
|||
|
Before Width: | Height: | Size: 149 KiB |
|
Before Width: | Height: | Size: 74 KiB After Width: | Height: | Size: 86 KiB |
Loading…
Reference in new issue