diff --git a/AI协助记录.txt b/AI协助记录.txt new file mode 100644 index 0000000..364b4d3 --- /dev/null +++ b/AI协助记录.txt @@ -0,0 +1,2 @@ +Prompt1:“把 Python 温度转换程序完整改成 Java,功能一致”;Prompt2:“改 Java 文件后缀.txt 为.java 提示文件不可用,怎么解决”;Prompt3:“javac 编译 Java 文件报编码 GBK 错误,怎么处理” +AI 帮助:如何使用git及安装JDK,提供符合要求的 Java 源码,解释编译 / 运行命令的区别,指导解决文件名、编码、目录路径导致的报错 \ No newline at end of file diff --git a/BankAccount.java b/BankAccount.java new file mode 100644 index 0000000..18f561d --- /dev/null +++ b/BankAccount.java @@ -0,0 +1,40 @@ +public class BankAccount { + private int accountNumber; + private String ownerName; + private double balance; + public BankAccount(int accountNumber,String ownerName,double balance) { + this.accountNumber = accountNumber; + this.balance = balance; + this.ownerName = ownerName; + } + public int getAccountNumber() { + return accountNumber; + } + public String getOwnerName() { + return ownerName; + } + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + } + public double getBalance() { + return balance; + } + //业务方法:存款 + public void deposit(double amount) { + if (amount > 0) { + balance += amount; + System.out.println("存款成功,当前余额: " + balance); + }else { + System.out.println("存款金额必须大于0"); + } + } + //业务方法:取款 + public void withdraw(double amount) { + if (amount > 0 && amount <= balance) { + balance -= amount ; + System.out.println("取款成功,当前余额: " + balance); + } else { + System.out.println("余额不足或金额无效"); + } + } +} \ No newline at end of file diff --git a/README.md.txt b/README.md.txt new file mode 100644 index 0000000..536f87f --- /dev/null +++ b/README.md.txt @@ -0,0 +1,22 @@ +# 温度转换程序作业说明 + +## 作业信息 +第一周课后作业:温度转换器 +功能:摄氏度 ↔ 华氏度 互相转换 + +## 文件说明 +TemperatureConverter.java - Java 源代码 +README.md - 本说明文件 + +## 编译命令 +javac TemperatureConverter.java + +## 运行命令 +java TemperatureConverter + +## 运行示例 +输入:36.6 C +输出:36.60 °C = 97.88 °F + +输入:97 F +输出:97.00 °F = 36.11 °C \ No newline at end of file diff --git a/TemperatureConverter.class b/TemperatureConverter.class new file mode 100644 index 0000000..d76f630 Binary files /dev/null and b/TemperatureConverter.class differ diff --git a/TemperatureConverter.java b/TemperatureConverter.java new file mode 100644 index 0000000..f4ccace --- /dev/null +++ b/TemperatureConverter.java @@ -0,0 +1,75 @@ +import java.util.Scanner; + +/** + * ¶ת + * ܣ֧϶(C)뻪϶(F)֮˫תPythonԭһµĽ߼ + */ +public class TemperatureConverter { + + /** + * ϶תΪ϶ + * @param c ¶ֵfloatͣ + * @return Ӧ϶ֵdoubleͣ + */ + public static double celsiusToFahrenheit(double c) { + return c * 9.0 / 5.0 + 32.0; + } + + /** + * ϶תΪ϶ + * @param f ¶ֵfloatͣ + * @return Ӧ¶ֵdoubleͣ + */ + public static double fahrenheitToCelsius(double f) { + return (f - 32.0) * 5.0 / 9.0; + } + + /** + * ڣʵPythonԭһµû߼ + */ + public static void main(String[] args) { + // Scannerû + Scanner scanner = new Scanner(System.in); + + // ʾû룬Pythonԭʾһ + System.out.print("Ҫת¶뵥λ 36.6 C 97 F"); + String s = scanner.nextLine().trim(); + + // + if (s.isEmpty()) { + System.out.println("Ϊգ˳"); + scanner.close(); + return; + } + + // ַ + String[] parts = s.split(" "); + try { + // ¶ֵĬϵλΪCPython߼һ£ + double value = Double.parseDouble(parts[0]); + String unit = parts.length > 1 ? parts[1].toUpperCase() : "C"; + + // ϶ת϶ + 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"); + } + } + // ֵ쳣ֵ + catch (Exception e) { + System.out.println("ʧܣ밴ʾֵ뵥λ磺36.6 C"); + } finally { + // رScannerԴ + scanner.close(); + } + } +} \ No newline at end of file diff --git a/w2/DataCleaner.java b/w2/DataCleaner.java new file mode 100644 index 0000000..e60be9b --- /dev/null +++ b/w2/DataCleaner.java @@ -0,0 +1,42 @@ +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 i = 0; i < sensorData.length; i++) { + // 获取当前遍历到的数组元素 + int currentData = sensorData[i]; + + // 第一步:检查是否是致命错误(999代表传感器掉线) + if (currentData == 999) { + System.out.println("致命错误:传感器掉线,终止处理"); + break; // 立即终止整个循环 + } + + // 第二步:检查是否是无效数据(0/负数/大于100的数) + if (currentData <= 0 || currentData > 100) { + System.out.println("警告:发现越界数据 " + currentData + ",已跳过"); + continue; // 跳过当前循环,处理下一个数据 + } + + // 第三步:能走到这里的都是有效数据(1-100之间) + validSum += currentData; // 累加有效数据总和 + validCount++; // 有效数据个数+1 + } + + // 循环结束后,处理最终结果 + if (validCount > 0) { + // 计算平均值:注意将int类型转换为double,避免整数除法陷阱 + double average = (double) validSum / validCount; + // 打印平均值,保留小数部分 + System.out.println("有效数据的平均值为:" + average); + } else { + // 没有有效数据的情况 + System.out.println("无有效数据"); + } + } +} diff --git a/w2/屏幕截图 2026-03-18 221124.png b/w2/屏幕截图 2026-03-18 221124.png new file mode 100644 index 0000000..75629f7 Binary files /dev/null and b/w2/屏幕截图 2026-03-18 221124.png differ diff --git a/屏幕截图 2026-03-25 200847.png b/屏幕截图 2026-03-25 200847.png new file mode 100644 index 0000000..0552e0d Binary files /dev/null and b/屏幕截图 2026-03-25 200847.png differ diff --git a/运行成功截图.png b/运行成功截图.png new file mode 100644 index 0000000..1a7a75a Binary files /dev/null and b/运行成功截图.png differ