4 changed files with 88 additions and 0 deletions
@ -0,0 +1,2 @@ |
|||
P:帮我写一份README。 |
|||
AI:执行。 |
|||
@ -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; |
|||
} |
|||
} |
|||
@ -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 |
|||
@ -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` 中调用。 |
|||
Loading…
Reference in new issue