wanglixia 2 weeks ago
parent
commit
b8caa8917d
  1. 30
      .gitignore
  2. 10
      .idea/.gitignore
  3. 6
      .idea/misc.xml
  4. 8
      .idea/modules.xml
  5. 7
      .idea/vcs.xml
  6. 2
      AI协助记录.txt
  7. 22
      README.md.txt
  8. BIN
      TemperatureConverter.class
  9. 75
      TemperatureConverter.java
  10. 1
      java
  11. 4
      src/w3/AI协助记录
  12. 40
      src/w3/BankAccount.java
  13. 80
      src/w3/Employee.java
  14. 24
      src/w3/TestEmployee.java
  15. BIN
      src/w3/屏幕截图 2026-03-25 223417.png
  16. 11
      untitled2.iml
  17. 42
      w2/DataCleaner.java
  18. BIN
      w2/屏幕截图 2026-03-18 221124.png
  19. BIN
      运行成功截图.png

30
.gitignore

@ -1,30 +0,0 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
.kotlin
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

10
.idea/.gitignore

@ -1,10 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

6
.idea/misc.xml

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/untitled2.iml" filepath="$PROJECT_DIR$/untitled2.iml" />
</modules>
</component>
</project>

7
.idea/vcs.xml

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/java" vcs="Git" />
</component>
</project>

2
AI协助记录.txt

@ -1,2 +0,0 @@
Prompt1:“把 Python 温度转换程序完整改成 Java,功能一致”;Prompt2:“改 Java 文件后缀.txt 为.java 提示文件不可用,怎么解决”;Prompt3:“javac 编译 Java 文件报编码 GBK 错误,怎么处理”
AI 帮助:如何使用git及安装JDK,提供符合要求的 Java 源码,解释编译 / 运行命令的区别,指导解决文件名、编码、目录路径导致的报错

22
README.md.txt

@ -1,22 +0,0 @@
# 温度转换程序作业说明
## 作业信息
第一周课后作业:温度转换器
功能:摄氏度 ↔ 华氏度 互相转换
## 文件说明
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

BIN
TemperatureConverter.class

Binary file not shown.

75
TemperatureConverter.java

@ -1,75 +0,0 @@
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 {
// 解析温度数值,默认单位为C(与Python逻辑一致)
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();
}
}
}

1
java

@ -1 +0,0 @@
Subproject commit 65fdb7f37fac6d3d2a86adb0aed61d17ae2eaac8

4
src/w3/AI协助记录

@ -1,4 +0,0 @@
AI协助记录
1.写了Employee类无法跑代码后了解TestEmployee测试类的作用和写法
2.IDEA初次使用,询问AI关于IDEA写代码的操作流程
3.AI帮助完成代码编写。

40
src/w3/BankAccount.java

@ -1,40 +0,0 @@
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("余额不足或金额无效");
}
}
}

80
src/w3/Employee.java

@ -1,80 +0,0 @@
package w3;
public class Employee {
// 静态变量:公司名称
public static String companyName;
// 成员变量
private String id; // 工号
private String name; // 姓名
private String department;// 部门
private double salary; // 工资
// 最低工资标准
private static final double MIN_SALARY = 2000;
// 构造方法:创建对象时赋值
public Employee(String id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
setSalary(salary); // 调用方法保证工资合法
}
// get/set 方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
if (salary >= MIN_SALARY) {
this.salary = salary;
} else {
this.salary = MIN_SALARY;
System.out.println("工资过低,自动调整为:" + MIN_SALARY);
}
}
// 涨薪方法
public void raiseSalary(double percent) {
double newSalary = salary * (1 + percent / 100);
setSalary(newSalary);
}
// 打印信息
@Override
public String toString() {
return "员工{" +
"公司='" + companyName + '\'' +
", 工号='" + id + '\'' +
", 姓名='" + name + '\'' +
", 部门='" + department + '\'' +
", 工资=" + salary +
'}';
}
}

24
src/w3/TestEmployee.java

@ -1,24 +0,0 @@
package w3;
public class TestEmployee {
public static void main(String[] args) {
// 1. 设置公司名称(静态变量)
Employee.companyName = "湖南科技有限公司";
// 2. 创建两个员工对象
Employee emp1 = new Employee("E001", "张三", "技术部", 2500);
Employee emp2 = new Employee("E002", "李四", "人事部", 1800);
// 3. 打印信息
System.out.println(emp1);
System.out.println(emp2);
// 4. 涨薪
emp1.raiseSalary(5);
emp2.raiseSalary(10);
System.out.println("\n=====涨薪后=====");
System.out.println(emp1);
System.out.println(emp2);
}
}

BIN
src/w3/屏幕截图 2026-03-25 223417.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 83 KiB

11
untitled2.iml

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

42
w2/DataCleaner.java

@ -1,42 +0,0 @@
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("无有效数据");
}
}
}

BIN
w2/屏幕截图 2026-03-18 221124.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

BIN
运行成功截图.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 81 KiB

Loading…
Cancel
Save