From 574d58c4b0083488fa43c8007a5c9e6397dfbb83 Mon Sep 17 00:00:00 2001 From: Liangkaixiong <2314785400@qq.com> Date: Tue, 17 Mar 2026 16:51:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w2'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w2/AI.txt | 2 ++ w2/Main.java | 27 +++++++++++++++++++++++++ w2/Output.txt | 3 +++ w2/README.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 w2/AI.txt create mode 100644 w2/Main.java create mode 100644 w2/Output.txt create mode 100644 w2/README.md diff --git a/w2/AI.txt b/w2/AI.txt new file mode 100644 index 0000000..10726d9 --- /dev/null +++ b/w2/AI.txt @@ -0,0 +1,2 @@ +P:帮我写一份README。 +AI:执行。 \ No newline at end of file diff --git a/w2/Main.java b/w2/Main.java new file mode 100644 index 0000000..8aedd8a --- /dev/null +++ b/w2/Main.java @@ -0,0 +1,27 @@ +public class Main { + public static void main(String[] args) { + Student s1 = new Student("2021001", "Alice", 85.5); + Student s2 = new Student("2021002", "Bob", 92.0); + double score1 = s1.study(); + double score2 = s2.study(); + System.out.println(s1.name + " scored: " + score1); + System.out.println(s2.name + " scored: " + score2); + } + +} + +class Student { + public String studentId; + public String name; + public double score; + + public Student(String studentId, String name, double score) { + this.studentId = studentId; + this.name = name; + this.score = score; + } + + public double study() { + return this.score; + } +} diff --git a/w2/Output.txt b/w2/Output.txt new file mode 100644 index 0000000..90a9f0b --- /dev/null +++ b/w2/Output.txt @@ -0,0 +1,3 @@ +PS D:\VisualStudioProgram\VSCodePrograms> cd "d:\VisualStudioProgram\VSCodePrograms\JavaLearningProject\w2\" ; if ($?) { javac Main.java } ; if ($?) { java Main } +Alice scored: 85.5 +Bob scored: 92.0 \ No newline at end of file diff --git a/w2/README.md b/w2/README.md new file mode 100644 index 0000000..dc70465 --- /dev/null +++ b/w2/README.md @@ -0,0 +1,56 @@ +# w2 模块说明 + +本目录包含一个简单的 Java 练习程序,演示了: + +- 如何定义类与对象 +- 如何在 `main` 方法中创建对象并调用其方法 + +## 目录结构 + +- `Main.java`:程序入口,包含 `main()` 方法。创建 `Student` 对象并调用 `study()` 方法。 + +## 编译与运行 + +### 直接编译运行(推荐,用于提交作业) + +在 `w2` 目录内执行: + +```powershell +javac Main.java +java Main +``` + +> 说明:当前 `Main.java` 中没有 `package` 声明,所以可以直接在文件所在目录运行。 + +### 其他目录(例如作业提交目录) + +如果你把 `Main.java` 复制到其他目录,只要在该目录下执行相同命令即可: + +```powershell +javac Main.java +java Main +``` + +## 程序说明 + +`Main` 类创建了两个学生对象,并调用 `Student.study()` 返回各自成绩: + +```java +Student s1 = new Student("2021001", "Alice", 85.5); +double score1 = s1.study(); +``` + +输出示例: + +``` +Alice scored: 85.5 +Bob scored: 92.0 +``` + +## 常见问题 + +- **报错:找不到主类** + - 请确认你当前目录是包含 `Main.java` 的目录,并且运行 `java Main`。 + +- **想让 `study` 方法接收参数或增加行为?** + - 可以在 `Student` 类中添加更多字段、方法,并在 `Main` 中调用。