Compare commits
No commits in common. 'main' and 'master' have entirely different histories.
8 changed files with 81 additions and 89 deletions
@ -0,0 +1,20 @@ |
|||
package java01; |
|||
|
|||
public class Main { |
|||
public static void main(String[] args) { |
|||
// 实例化第一个学生对象
|
|||
Student stu1 = new Student(03030325, "小A", 92.5); |
|||
stu1.study(); |
|||
System.out.println("学号:" + stu1.getStudentId()); |
|||
System.out.println("姓名:" + stu1.getName()); |
|||
System.out.println("分数:" + stu1.getScore()); |
|||
System.out.println("------------------------"); |
|||
|
|||
// 实例化第二个学生对象
|
|||
Student stu2 = new Student(33030326, "小B", 88.0); |
|||
stu2.study(); |
|||
System.out.println("学号:" + stu2.getStudentId()); |
|||
System.out.println("姓名:" + stu2.getName()); |
|||
System.out.println("分数:" + stu2.getScore()); |
|||
} |
|||
} |
|||
@ -1,11 +0,0 @@ |
|||
# 温度转换程序作业提交 |
|||
## 作业基础信息 |
|||
- **项目名称**:TemperatureConverter |
|||
- **开发环境**:Eclipse IDE + Java JDK 17 |
|||
- **核心功能**:实现摄氏度(C)与华氏度(F)双向转换,完全等效于原Python程序逻辑 |
|||
|
|||
## 编译与运行说明 |
|||
### Eclipse 运行 |
|||
1. 右键点击 `src/java01/TemperatureConverter.java` 文件; |
|||
2. 选择 `Run As` → `Java Application`; |
|||
3. 在底部 Console 面板输入格式:`数值 单位`(例:36.6 C),按回车查看结果。 |
|||
@ -0,0 +1,33 @@ |
|||
package java01; |
|||
|
|||
public class Student { |
|||
// 1. 定义三个私有属性
|
|||
private int studentId; |
|||
private String name; |
|||
private double score; |
|||
|
|||
// 2. 构造方法:名字必须和类名完全一样
|
|||
public Student(int studentId, String name, double score) { |
|||
this.studentId = studentId; |
|||
this.name = name; |
|||
this.score = score; |
|||
} |
|||
|
|||
// 3. study 方法
|
|||
public void study() { |
|||
System.out.println(name + " 正在学习..."); |
|||
} |
|||
|
|||
// 4. Getter 方法(用来获取私有属性的值)
|
|||
public int getStudentId() { |
|||
return studentId; |
|||
} |
|||
|
|||
public String getName() { |
|||
return name; |
|||
} |
|||
|
|||
public double getScore() { |
|||
return score; |
|||
} |
|||
} |
|||
@ -1,78 +0,0 @@ |
|||
package java01; |
|||
import java.util.Scanner; |
|||
|
|||
/** |
|||
* 温度转换器示例程序(Java 版) |
|||
* 支持摄氏度(C)与华氏度(F)之间互转 |
|||
* 与你提供的 Python 程序功能、逻辑完全一致 |
|||
*/ |
|||
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; |
|||
} |
|||
|
|||
/** |
|||
* 主逻辑:读取输入、解析、判断单位、转换并输出 |
|||
*/ |
|||
public static void main(String[] args) { |
|||
Scanner scanner = new Scanner(System.in); |
|||
|
|||
// 提示用户输入,格式示例:"36.6 C" 或 "97 F"
|
|||
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("\\s+"); |
|||
double value; |
|||
String unit; |
|||
|
|||
try { |
|||
// 解析数值与单位
|
|||
value = Double.parseDouble(parts[0]); |
|||
if (parts.length > 1) { |
|||
unit = parts[1].toUpperCase(); |
|||
} else { |
|||
unit = "C"; // 默认单位 C
|
|||
} |
|||
} catch (Exception e) { |
|||
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); |
|||
scanner.close(); |
|||
return; |
|||
} |
|||
|
|||
// 根据单位转换
|
|||
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。"); |
|||
} |
|||
|
|||
scanner.close(); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
# -*- coding: utf-8 -*- |
|||
""" |
|||
Created on Sun Mar 15 21:26:00 2026 |
|||
|
|||
@author: lenovo |
|||
""" |
|||
|
|||
class Student: |
|||
def __init__(self, student_id: int, name: str, score: float): |
|||
# 对应 Java 里的 private 成员变量 |
|||
self.student_id = student_id |
|||
self.name = name |
|||
self.score = score |
|||
|
|||
def study(self): |
|||
# 对应 Java 里的 study() 方法 |
|||
print(f"{self.name} 正在学习...") |
|||
|
|||
# 对应 Java 里的 Getter 方法 |
|||
def get_student_id(self): |
|||
return self.student_id |
|||
|
|||
def get_name(self): |
|||
return self.name |
|||
|
|||
def get_score(self): |
|||
return self.score |
|||
|
Before Width: | Height: | Size: 360 KiB |
@ -0,0 +1 @@ |
|||
通过将 Python 代码转换为 Java 代码,先了解了Python 开发更灵活,而 Java 在使用过程中必须显式声明数据类型。这能在编译阶段提前发现类型错误,提升代码安全性和运行效率。访问修饰符(如 public、private)是 Java 封装思想的核心体现。将成员变量设为 private,通过 public 的 getter/setter 方法访问,能避免外部代码随意修改内部状态,让代码更健壮、易维护。这次实践让我理解了 Java 严谨设计的意义,也加深了对面向对象封装原则的认识。 |
|||
Loading…
Reference in new issue