java学生选课系统输出有两名同学选的课程的信息
时间: 2024-05-12 12:20:51 浏览: 75
以下是一种可能的Java代码实现,用于输出有两名同学选的课程信息:
```java
import java.util.*;
public class CourseSelectionSystem {
public static void main(String[] args) {
// 定义学生和课程信息
String[][] students = {{"Tom", "Math", "English", "History"},
{"Jerry", "Math", "Art", "Geography"},
{"Alice", "Art", "History", "Biology"},
{"Bob", "Math", "English", "Physics"}};
// 学生人数
int numStudents = students.length;
// 课程列表
List<String> courses = new ArrayList<>();
for (int i = 0; i < numStudents; i++) {
for (int j = 1; j < students[i].length; j++) {
String course = students[i][j];
if (!courses.contains(course)) {
courses.add(course);
}
}
}
// 输出所有课程信息
System.out.println("All courses:");
for (String course : courses) {
System.out.println(course);
}
// 查找有两名同学选的课程
System.out.println("\nCourses selected by two students:");
for (String course : courses) {
int count = 0;
String studentsSelected = "";
for (int i = 0; i < numStudents; i++) {
if (Arrays.asList(students[i]).contains(course)) {
count++;
studentsSelected += students[i][0] + ", ";
}
}
if (count == 2) {
System.out.println(course + " selected by " + studentsSelected);
}
}
}
}
```
上述代码首先定义了一个二维字符串数组,表示每个学生选的课程信息。然后通过遍历数组,获取所有课程信息并存入一个列表中。接着,遍历课程列表,查找有两名同学选的课程。对于每个课程,遍历学生数组,统计选这门课程的学生人数,并记录他们的姓名。如果统计结果为2,则输出这门课程和选它的两名学生的姓名。最终输出所有课程信息和选了两门及以上课程的学生的信息。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)