20 changed files with 96 additions and 141 deletions
@ -1,3 +0,0 @@ |
|||
本次作业由AI协助完成,我全程参与学习与实践。 |
|||
在AI指导下,我了解了Python与Java的基础语法差异,一步步完成温度转换程序的移植,学习了Java注释、变量、方法等基础用法。 |
|||
我按照要求编写规范注释,测试基础交互功能,在AI帮助下理解命令行参数与文件批量处理逻辑,并亲手调试运行,完成三项功能测试与结果记录。 |
|||
@ -1,28 +0,0 @@ |
|||
\#温度转换程序 |
|||
|
|||
|
|||
|
|||
编译命令: |
|||
|
|||
javac TemperatureConverter.java |
|||
|
|||
|
|||
|
|||
运行命令: |
|||
|
|||
1\.交互模式: |
|||
|
|||
java TemperatureConverter |
|||
|
|||
2\.命令行模式: |
|||
|
|||
java TemperatureConverter 36.6 C |
|||
|
|||
3\.文件批量: |
|||
|
|||
java TemperatureConverter -file temps.txt |
|||
|
|||
|
|||
|
|||
功能:实现摄氏与华氏温度互相转换 |
|||
|
|||
@ -1,105 +0,0 @@ |
|||
import java.util.Scanner; |
|||
import java.io.File; |
|||
import java.io.FileNotFoundException; |
|||
/** |
|||
* 温度转换器 |
|||
* 支持摄氏度(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; |
|||
} |
|||
/** |
|||
* 统一执行温度转换并输出结果 |
|||
* @param value 温度数值 |
|||
* @param unit 温度单位(C/F) |
|||
*/ |
|||
public static void convert(double value, String unit) { |
|||
if (unit.startsWith("C")) { |
|||
double f = celsiusToFahrenheit(value); |
|||
System.out.printf("%.2f °C = %.2f °F\n", value, f); |
|||
} else if (unit.startsWith("F")) { |
|||
double c = fahrenheitToCelsius(value); |
|||
System.out.printf("%.2f °F = %.2f °C\n", value, c); |
|||
} else { |
|||
System.out.println("未知单位,请使用 C 或 F。"); |
|||
} |
|||
} |
|||
/** |
|||
* 从文件批量读取温度并转换 |
|||
* @param fileName 待读取的文件名 |
|||
*/ |
|||
public static void batchConvert(String fileName) { |
|||
try { |
|||
Scanner fileScanner = new Scanner(new File(fileName)); |
|||
System.out.println("===== 批量转换结果 ====="); |
|||
while (fileScanner.hasNextLine()) { |
|||
String line = fileScanner.nextLine().trim(); |
|||
if (line.isEmpty()) continue; |
|||
String[] parts = line.split("\\s+"); |
|||
double val = Double.parseDouble(parts[0]); |
|||
String unit = parts[1].toUpperCase(); |
|||
convert(val, unit); |
|||
} |
|||
fileScanner.close(); |
|||
} catch (FileNotFoundException e) { |
|||
System.out.println("文件不存在:" + fileName); |
|||
} |
|||
} |
|||
/** |
|||
* 程序主入口 |
|||
*/ |
|||
public static void main(String[] args) { |
|||
// 优先处理文件批量转换指令
|
|||
if (args.length == 2 && args[0].equals("-file")) { |
|||
batchConvert(args[1]); |
|||
return; |
|||
} |
|||
// 处理普通命令行参数
|
|||
if (args.length == 2) { |
|||
try { |
|||
double val = Double.parseDouble(args[0]); |
|||
String unit = args[1].toUpperCase(); |
|||
convert(val, unit); |
|||
return; |
|||
} catch (Exception e) { |
|||
System.out.println("命令行参数错误!示例:36.6 C"); |
|||
return; |
|||
} |
|||
} |
|||
// 交互模式(无参数时进入)
|
|||
Scanner scanner = new Scanner(System.in); |
|||
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
|||
String s = scanner.nextLine().trim(); |
|||
if (s.isEmpty()) { |
|||
System.out.println("输入为空,程序退出。"); |
|||
return; |
|||
} |
|||
String[] parts = s.split(" "); |
|||
double value; |
|||
String unit; |
|||
try { |
|||
value = Double.parseDouble(parts[0]); |
|||
unit = parts.length > 1 ? parts[1].toUpperCase() : "C"; |
|||
} catch (Exception e) { |
|||
System.out.println("输入解析失败,请按示例输入:36.6 C"); |
|||
return; |
|||
} |
|||
convert(value, unit); |
|||
scanner.close(); |
|||
} |
|||
} |
|||
@ -1,5 +0,0 @@ |
|||
36.5 C |
|||
98 F |
|||
0 C |
|||
100 C |
|||
212 F |
|||
|
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 87 KiB |
|
Before Width: | Height: | Size: 71 KiB After Width: | Height: | Size: 71 KiB |
@ -0,0 +1,4 @@ |
|||
在本次Java开发中,我让AI为银行账户类 BankAccount 生成测试用例, |
|||
以验证账户创建、姓名修改、存取款等功能。 |
|||
在编译运行时,我遇到了拼写错误、格式缺失、方法未定义等语法问题, |
|||
AI逐一帮我定位并修正,同时指导我完成命令行编译与运行,最终成功执行测试用例。 |
|||
@ -0,0 +1,49 @@ |
|||
public class BankAccount { |
|||
private String accountNumber; |
|||
private String ownerName; |
|||
private double balance; |
|||
|
|||
public BankAccount(String accountNumber,String ownerName){ |
|||
this.accountNumber=accountNumber; |
|||
this.ownerName=ownerName; |
|||
this.balance=0.0; |
|||
} |
|||
|
|||
public String getAccountNumber(){ |
|||
return accountNumber; |
|||
} |
|||
|
|||
public void setOwnerName(String ownerName){ |
|||
this.ownerName=ownerName; |
|||
} |
|||
|
|||
public String getOwnerName(){ |
|||
return ownerName; |
|||
} |
|||
|
|||
public double getBalance(){ |
|||
return balance; |
|||
} |
|||
|
|||
public void deposit(double amount){ |
|||
if (amount>0){ |
|||
this.balance+=amount; |
|||
System.out.println("存款成功!存入金额:"+amount+"元,当前余额:"+this.balance+"元"); |
|||
}else { |
|||
System.out.println("存款失败!存款金额必须大于0"); |
|||
} |
|||
} |
|||
|
|||
public void withdraw(double amount){ |
|||
if (amount<=0){ |
|||
System.out.println("取款失败!取款金额必须大于0"); |
|||
} else if (amount>this.balance){ |
|||
System.out.println("取款失败!余额不足,当前余额:"+this.balance+"元"); |
|||
} else { |
|||
this.balance-=amount; |
|||
System.out.println("取款成功!取出金额:"+amount+"元,当前余额:"+this.balance+"元"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
@ -0,0 +1,18 @@ |
|||
功能: |
|||
|
|||
实现一个银行账户类 BankAccount, |
|||
|
|||
支持账户创建、户主姓名设置、存款、取款和余额查询,并对非法金额、余额不足等情况做异常判断。 |
|||
|
|||
|
|||
|
|||
编译命令: |
|||
|
|||
javac BankAccount.java TestBankAccount.java |
|||
|
|||
|
|||
|
|||
运行命令: |
|||
|
|||
java TestBankAccount |
|||
|
|||
@ -0,0 +1,25 @@ |
|||
public class TestBankAccount{ |
|||
//测试方法(main方法,用于验证功能)
|
|||
public static void main(String[] args) { |
|||
// 创建账户
|
|||
BankAccount account = new BankAccount("622202123456789", "张三"); |
|||
|
|||
// 测试查询
|
|||
System.out.println("账户号:" + account.getAccountNumber()); |
|||
System.out.println("户主姓名:" + account.getOwnerName()); |
|||
System.out.println("初始余额:" + account.getBalance()); |
|||
|
|||
// 测试修改户主姓名
|
|||
account.setOwnerName("李四"); |
|||
System.out.println("修改后户主姓名:" + account.getOwnerName()); |
|||
|
|||
// 测试存款
|
|||
account.deposit(1000); |
|||
account.deposit(-500); // 测试无效存款
|
|||
|
|||
// 测试取款
|
|||
account.withdraw(300); |
|||
account.withdraw(800); // 测试余额不足
|
|||
account.withdraw(-200); // 测试无效取款
|
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 81 KiB |
|
Before Width: | Height: | Size: 87 KiB |
Loading…
Reference in new issue