每个编辑单元只能有一个public class

用public修饰的类, 它所在的JAVA文件名必须和类名一样,而且只能有一个public class

比如public class A

那么它所在的文件必须是A.java 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的学生选课系统的 C++ 代码,带有注释说明每个步骤的作用: ```c++ #include <iostream> #include <vector> #include <algorithm> // STL 中的算法库,用于查找元素 using namespace std; // 学生类定义 class Student { public: string name; // 学生姓名 vector<string> courses; // 学生已选课程 }; // 课程类定义 class Course { public: string name; // 课程名称 int capacity; // 课程容量 vector<Student*> students; // 已选该课程的学生指针列表 }; // 选课系统类定义 class CourseSelectionSystem { private: vector<Student*> students; // 所有学生指针列表 vector<Course*> courses; // 所有课程指针列表 public: // 添加学生 void addStudent(string name) { Student* newStudent = new Student(); newStudent->name = name; students.push_back(newStudent); } // 添加课程 void addCourse(string name, int capacity) { Course* newCourse = new Course(); newCourse->name = name; newCourse->capacity = capacity; courses.push_back(newCourse); } // 学生选课 void enrollCourse(string studentName, string courseName) { // 查找该学生 auto it = find_if(students.begin(), students.end(), [&](Student* s) { return s->name == studentName; }); if (it == students.end()) { cout << "Error: Cannot find student " << studentName << endl; return; } Student* student = *it; // 查找该课程 auto it2 = find_if(courses.begin(), courses.end(), [&](Course* c) { return c->name == courseName; }); if (it2 == courses.end()) { cout << "Error: Cannot find course " << courseName << endl; return; } Course* course = *it2; // 判断该课程容量是否已满 if (course->students.size() >= course->capacity) { cout << "Error: Course " << courseName << " is already full" << endl; return; } // 判断该学生是否已选该课程 auto it3 = find_if(course->students.begin(), course->students.end(), [&](Student* s) { return s->name == studentName; }); if (it3 != course->students.end()) { cout << "Error: Student " << studentName << " has already enrolled in course " << courseName << endl; return; } // 将该学生加入该课程的已选学生列表中,将该课程加入该学生的已选课程列表中 course->students.push_back(student); student->courses.push_back(courseName); cout << "Enroll course " << courseName << " for student " << studentName << " successfully" << endl; } // 显示所有学生及其已选课程 void showStudents() { cout << "Students list:" << endl; for (auto student : students) { cout << student->name << ":"; for (auto courseName : student->courses) { cout << " " << courseName; } cout << endl; } } // 显示所有课程及其已选学生 void showCourses() { cout << "Courses list:" << endl; for (auto course : courses) { cout << course->name << ":"; for (auto student : course->students) { cout << " " << student->name; } cout << endl; } } }; int main() { CourseSelectionSystem system; // 添加学生 system.addStudent("Alice"); system.addStudent("Bob"); system.addStudent("Charlie"); // 添加课程 system.addCourse("Math", 2); system.addCourse("English", 1); system.addCourse("Computer Science", 3); // 学生选课 system.enrollCourse("Alice", "Math"); system.enrollCourse("Bob", "Math"); system.enrollCourse("Charlie", "Math"); system.enrollCourse("Charlie", "English"); system.enrollCourse("Charlie", "Computer Science"); // 显示所有学生及其已选课程 system.showStudents(); // 显示所有课程及其已选学生 system.showCourses(); return 0; } ``` 该系统的主要功能是添加学生、添加课程和学生选课,同时还提供了两个方法用于显示所有学生及其已选课程和所有课程及其已选学生。在学生选课时,该系统会进行多项检查,如是否存在该学生或该课程,该课程容量是否已满,该学生是否已选该课程等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值