diff --git a/TemperatureConventer.java.txt b/TemperatureConventer.java.txt deleted file mode 100644 index a4d0581..0000000 --- a/TemperatureConventer.java.txt +++ /dev/null @@ -1,63 +0,0 @@ -// 文件名: TemperatureConverter.java -// 这是一个温度转换程序,可以将摄氏温度和华氏温度互相转换 - -import java.util.Scanner; // 导入Scanner类,用来读取用户在键盘上的输入 - -/** - * 主类名,必须和文件名一致 - */ -public class TemperatureConverter { - - /** - * 主方法:程序的入口点 - * @param args 命令行参数(虽然本次未使用,但这是Java的固定格式)[citation:3] - */ - public static void main(String[] args) { - // 1. 创建Scanner对象,用于接收用户的输入 - Scanner scanner = new Scanner(System.in); - - // 2. 提示用户输入 - System.out.println("请输入温度值及其单位(例如:36.6 C 或 98 F),用空格隔开:"); - - // 3. 读取用户输入 - double temperature = scanner.nextDouble(); // 读取第一个数字(温度值) - String unit = scanner.next(); // 读取第二个字符串(单位 C 或 F) - - // 4. 关闭scanner - scanner.close(); - - // 5. 根据单位进行判断和转换 - if (unit.equalsIgnoreCase("C")) { // 如果用户输入的是 C (忽略大小写) - // 调用 celsiusToFahrenheit 方法进行转换 - double converted = celsiusToFahrenheit(temperature); - System.out.println(temperature + "°C 转换为华氏温度是:" + converted + "°F"); - } else if (unit.equalsIgnoreCase("F")) { // 如果用户输入的是 F - // 调用 fahrenheitToCelsius 方法进行转换 - double converted = fahrenheitToCelsius(temperature); - System.out.println(temperature + "°F 转换为摄氏温度是:" + converted + "°C"); - } else { - // 如果用户输入的不是C或F - System.out.println("输入错误,单位只能是 C 或 F。"); - } - } - - /** - * 将摄氏温度转换为华氏温度 - * @param celsius 摄氏温度值 - * @return 计算后的华氏温度值 - */ - public static double celsiusToFahrenheit(double celsius) { - // 转换公式:华氏度 = 摄氏度 × 9/5 + 32 - return celsius * 9 / 5 + 32; - } - - /** - * 将华氏温度转换为摄氏温度 - * @param fahrenheit 华氏温度值 - * @return 计算后的摄氏温度值 - */ - public static double fahrenheitToCelsius(double fahrenheit) { - // 转换公式:摄氏度 = (华氏度 - 32) × 5/9 - return (fahrenheit - 32) * 5 / 9; - } -} \ No newline at end of file