Compare commits
2 Commits
d351274ccb
...
c80cb5ac3b
| Author | SHA1 | Date |
|---|---|---|
|
|
c80cb5ac3b | 4 weeks ago |
|
|
9346bde68c | 4 weeks ago |
4 changed files with 80 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,47 @@ |
|||
/** |
|||
* 温度转换程序 |
|||
* 支持摄氏度和华氏度的互相转换 |
|||
* @author 陈全文 |
|||
* @version 1.0 |
|||
*/ |
|||
import java.util.Scanner; |
|||
|
|||
public class TemperatureConverter { |
|||
|
|||
public static double celsiusToFahrenheit(double celsius) { |
|||
return celsius * 9.0 / 5.0 + 32.0; |
|||
} |
|||
|
|||
public static double fahrenheitToCelsius(double fahrenheit) { |
|||
return (fahrenheit - 32.0) * 5.0 / 9.0; |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
Scanner scanner = new Scanner(System.in); |
|||
|
|||
System.out.println("===== 温度转换程序 ====="); |
|||
System.out.println("1. 摄氏度 -> 华氏度"); |
|||
System.out.println("2. 华氏度 -> 摄氏度"); |
|||
System.out.print("请选择转换类型 (1/2): "); |
|||
|
|||
String choice = scanner.nextLine(); |
|||
|
|||
if (choice.equals("1")) { |
|||
System.out.print("请输入摄氏度: "); |
|||
double celsius = scanner.nextDouble(); |
|||
double fahrenheit = celsiusToFahrenheit(celsius); |
|||
System.out.println(celsius + "°C = " + fahrenheit + "°F"); |
|||
|
|||
} else if (choice.equals("2")) { |
|||
System.out.print("请输入华氏度: "); |
|||
double fahrenheit = scanner.nextDouble(); |
|||
double celsius = fahrenheitToCelsius(fahrenheit); |
|||
System.out.println(fahrenheit + "°F = " + celsius + "°C"); |
|||
|
|||
} else { |
|||
System.out.println("无效选择,请输入1或2"); |
|||
} |
|||
|
|||
scanner.close(); |
|||
} |
|||
} |
|||
Binary file not shown.
@ -0,0 +1,33 @@ |
|||
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 value : sensorData) { |
|||
if (value == 999) { |
|||
System.out.println("致命错误:传感器掉线,终止处理"); |
|||
break; |
|||
} |
|||
if (value >= 1 && value <= 100) { |
|||
// 正常范围
|
|||
validSum += value; |
|||
validCount++; |
|||
} else { |
|||
// 无效数据:0、负数或大于100(且不是999,因为999已提前处理)
|
|||
System.out.println("警告:发现越界数据 [" + value + "],已跳过"); |
|||
continue; // 可省略,但为符合题意显式写出
|
|||
} |
|||
} |
|||
|
|||
// 输出结果
|
|||
if (validCount > 0) { |
|||
double average = (double) validSum / validCount; |
|||
System.out.println("有效数据的平均值为:" + average); |
|||
} else { |
|||
System.out.println("无有效数据"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue