Java代码:学生选课信息
时间: 2023-12-18 13:03:33 浏览: 42
以下是一个简单的学生选课信息的Java代码示例:
```java
import java.util.ArrayList;
public class Student {
private String name;
private ArrayList<String> courses;
public Student(String name) {
this.name = name;
this.courses = new ArrayList<String>();
}
public String getName() {
return name;
}
public ArrayList<String> getCourses() {
return courses;
}
public void addCourse(String course) {
courses.add(course);
}
public void removeCourse(String course) {
courses.remove(course);
}
public static void main(String[] args) {
Student student1 = new Student("Tom");
student1.addCourse("Math");
student1.addCourse("English");
student1.addCourse("History");
Student student2 = new Student("Jerry");
student2.addCourse("Science");
student2.addCourse("English");
System.out.println(student1.getName() + "'s courses: " + student1.getCourses());
System.out.println(student2.getName() + "'s courses: " + student2.getCourses());
student1.removeCourse("History");
System.out.println(student1.getName() + "'s courses after dropping History: " + student1.getCourses());
}
}
```
该代码定义了一个名为“Student”的类,其中包含一个学生的姓名和所选课程的列表。该类还包括添加和删除课程的方法。
在“main”方法中,创建了两个学生对象,并向它们的课程列表中添加了一些课程。然后输出每个学生的姓名和所选课程,并演示了删除一个课程的过程。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)