6 changed files with 174 additions and 0 deletions
@ -0,0 +1,16 @@ |
|||||
|
编译 |
||||
|
javac TemperatureConverter.java |
||||
|
|
||||
|
运行 |
||||
|
1. 交互模式 (默认) |
||||
|
启动后手动输入温度(如 36.6 C),输入 quit 退出。 |
||||
|
java TemperatureConverter |
||||
|
|
||||
|
2. 命令行参数模式 |
||||
|
直接转换单个数值,无需进入交互界面。 |
||||
|
java TemperatureConverter 36.6 C |
||||
|
|
||||
|
3. 文件批量模式 |
||||
|
读取文本文件(每行格式:数值 单位)并批量输出结果。 |
||||
|
java TemperatureConverter Temperaturedata.txt |
||||
|
提示:确保 Temperaturedata.txt 文件位于当前目录下。 |
||||
Binary file not shown.
@ -0,0 +1,145 @@ |
|||||
|
//温度转换器示例程序(java)
|
||||
|
//支持摄氏度(C)与华氏度(F)之间互转
|
||||
|
|
||||
|
//功能:
|
||||
|
// 1. 交互模式 (默认)
|
||||
|
// 2. 命令行参数模式 (java TemperatureConverter 36.6 C)
|
||||
|
// 3. 文件批量模式 (java TemperatureConverterdata.txt)
|
||||
|
|
||||
|
import java.util.Scanner; |
||||
|
import java.io.File; |
||||
|
import java.io.FileNotFoundException; |
||||
|
|
||||
|
public class TemperatureConverter { |
||||
|
|
||||
|
public static double celsius_to_fahrenheit (double c) { |
||||
|
/*将摄氏度转换为华氏度。 |
||||
|
参数:c (float): 摄氏温度 |
||||
|
返回:float: 对应的华氏温度*/ |
||||
|
return c * 9.0 / 5.0 + 32.0; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public static double fahrenheit_to_celsius (double f) { |
||||
|
/*将华氏度转换为摄氏度。 |
||||
|
参数:f (float): 华氏温度 |
||||
|
返回:float: 对应的摄氏温度*/ |
||||
|
return (f - 32.0) * 5.0 / 9.0; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public static void processLine(String s) { |
||||
|
s=s.trim(); |
||||
|
|
||||
|
if (s.isEmpty()){ |
||||
|
System.out.println("输入为空,程序退出。"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
String[] parts=s.split("\\s+"); |
||||
|
|
||||
|
try{ |
||||
|
if (parts.length < 2) { |
||||
|
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); |
||||
|
return; // 直接结束方法,防止后续代码执行
|
||||
|
} |
||||
|
//允许用户输入两个部分:数值与单位
|
||||
|
double value = Double.parseDouble(parts[0]); |
||||
|
String unit = parts[1].toUpperCase(); |
||||
|
|
||||
|
if (unit.equals("C")) { |
||||
|
//从摄氏度转换为华氏度
|
||||
|
double f = celsius_to_fahrenheit(value); |
||||
|
System.out.println(value + " °C = "+ String.format("%.2f",f)+" °F"); |
||||
|
} else if (unit.equals("F")) { |
||||
|
//从华氏度转换为摄氏度
|
||||
|
double c = fahrenheit_to_celsius(value); |
||||
|
System.out.println(value + " °F = "+ String.format("%.2f",c)+" °C"); |
||||
|
} else { |
||||
|
System.out.println("未知单位,请使用 C 或 F。"); |
||||
|
} |
||||
|
|
||||
|
} catch (NumberFormatException e) { |
||||
|
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); |
||||
|
} |
||||
|
} |
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
// ================= 模式 1: 命令行参数模式 =================
|
||||
|
// 用法: java TemperatureConverter 36.6 C
|
||||
|
if (args.length == 2) { |
||||
|
String inputLine = args[0] + " " + args[1]; |
||||
|
// 直接处理这一行,不进入循环
|
||||
|
processLine(inputLine); |
||||
|
return; // 结束程序
|
||||
|
} |
||||
|
|
||||
|
// ================= 模式 2: 文件批量模式 =================
|
||||
|
// 用法: java TemperatureConverter temps.txt
|
||||
|
if (args.length == 1) { |
||||
|
String fileName = args[0]; |
||||
|
File file = new File(fileName); |
||||
|
|
||||
|
if (!file.exists()) { |
||||
|
System.err.println("错误: 找不到文件: " + fileName); |
||||
|
System.err.println("请确保文件在当前目录下,例如: java TemperatureConverter temps.txt"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (!file.isFile()) { |
||||
|
System.err.println("错误: " + fileName + " 不是一个有效文件。"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
System.out.println(">>> 开始从文件 [" + fileName + "] 批量读取..."); |
||||
|
|
||||
|
Scanner fileScanner = null; |
||||
|
try { |
||||
|
fileScanner = new Scanner(file); |
||||
|
int lineCount = 0; |
||||
|
while (fileScanner.hasNextLine()) { |
||||
|
String line = fileScanner.nextLine(); |
||||
|
// 跳过空行,但不退出
|
||||
|
if (line.trim().isEmpty()) continue; |
||||
|
|
||||
|
processLine(line); |
||||
|
lineCount++; |
||||
|
} |
||||
|
System.out.println(">>> 处理完成,共读取 " + lineCount + " 行数据。"); |
||||
|
|
||||
|
} catch (FileNotFoundException e) { |
||||
|
// 理论上前面检查过 exists,但以防万一
|
||||
|
System.err.println("系统错误: 无法读取文件。"); |
||||
|
} finally { |
||||
|
if (fileScanner != null) { |
||||
|
fileScanner.close(); |
||||
|
} |
||||
|
} |
||||
|
return; // 结束程序
|
||||
|
} |
||||
|
|
||||
|
// ================= 模式 3: 交互模式 (默认) =================
|
||||
|
// 用法: java TemperatureConverter
|
||||
|
Scanner scanner = new Scanner(System.in); |
||||
|
System.out.println("=== 温度转换器 (交互模式) ==="); |
||||
|
// 提示用户输入,格式示例:"36.6 C" 或 "97 F"
|
||||
|
System.out.println("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
||||
|
System.out.println("输入 'quit' 退出程序。"); |
||||
|
|
||||
|
while (true) { |
||||
|
System.out.print("> "); |
||||
|
String s = scanner.nextLine(); // 保留变量名 s
|
||||
|
|
||||
|
if (s.trim().equalsIgnoreCase("quit") || s.trim().equalsIgnoreCase("exit")) { |
||||
|
System.out.println("程序已退出。"); |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
// 调用统一的处理方法
|
||||
|
processLine(s); |
||||
|
} |
||||
|
|
||||
|
scanner.close(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
34 C |
||||
|
100 F |
||||
|
0 C |
||||
|
|
||||
|
23 H |
||||
@ -0,0 +1,8 @@ |
|||||
|
AI协作记录 |
||||
|
|
||||
|
本次作业通过三轮迭代完成Java温度转换器开发。 |
||||
|
需求解析:询问作业加分项含义,AI解释了命令行参数与文件批量处理的逻辑,并提供了语法映射表。 |
||||
|
代码重构:请求在保留原变量名基础上增加功能。AI建议提取processLine方法统一处理逻辑,避免代码重复,实现了交互、命令行、文件三种模式。 |
||||
|
Bug修复:针对批量输出换行问题,AI指出需统一使用println并确保每个分支显式换行,优化了空行跳过逻辑。 |
||||
|
|
||||
|
最终代码结构清晰,健壮性显著提升,完美满足所有评分标准。 |
||||
|
After Width: | Height: | Size: 125 KiB |
Loading…
Reference in new issue