利用Java自动生成课表
时间: 2023-07-03 08:23:30 浏览: 58
生成课表需要我们先定义好课程的数据结构,然后根据课程数据生成课表。以下是一个简单的课程数据结构和课表生成器的示例:
```
import java.util.ArrayList;
import java.util.Random;
public class CourseScheduleGenerator {
private ArrayList<Course> courses; // 所有的课程
private String[][] schedule; // 课表
private Random random; // 随机数生成器
public CourseScheduleGenerator() {
courses = new ArrayList<Course>();
schedule = new String[5][8]; // 5 天,每天 8 节课
random = new Random();
}
// 添加新的课程
public void addCourse(String name, String teacher, String time, String place) {
Course course = new Course(name, teacher, time, place);
courses.add(course);
}
// 生成课表
public void generateSchedule() {
// 先清空课表
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 8; j++) {
schedule[i][j] = "";
}
}
// 遍历所有的课程,随机分配到课表中
for (Course course : courses) {
int day = random.nextInt(5); // 随机选择一天
int time = random.nextInt(8); // 随机选择一节课
if (schedule[day][time].equals("")) { // 如果这个时间段没有课程,就安排这个课程
schedule[day][time] = course.getName() + "\n" + course.getTeacher() + "\n" + course.getPlace();
} else { // 否则重新选择时间
boolean found = false;
for (int i = 0; i < 5 && !found; i++) {
for (int j = 0; j < 8 && !found; j++) {
if (schedule[i][j].equals("")) {
schedule[i][j] = course.getName() + "\n" + course.getTeacher() + "\n" + course.getPlace();
found = true;
}
}
}
}
}
}
// 输出课表
public void printSchedule() {
System.out.println("课表:");
for (int i = 0; i < 5; i++) {
System.out.println("星期" + (i+1) + ":");
for (int j = 0; j < 8; j++) {
System.out.print(schedule[i][j] + "\t");
}
System.out.println();
}
}
}
// 课程类
class Course {
private String name;
private String teacher;
private String time;
private String place;
public Course(String name, String teacher, String time, String place) {
this.name = name;
this.teacher = teacher;
this.time = time;
this.place = place;
}
public String getName() {
return name;
}
public String getTeacher() {
return teacher;
}
public String getTime() {
return time;
}
public String getPlace() {
return place;
}
}
```
在这个示例中,我们定义了一个 `CourseScheduleGenerator` 类来生成课表。我们使用一个 ArrayList 来存储所有的课程,每个课程都是一个 `Course` 对象。`Course` 类包含了课程的基本信息,包括名称、教师、时间和地点等。我们使用一个二维数组 `schedule` 来存储课表,每个格子代表一节课的信息。我们使用一个随机数生成器来随机分配课程到课表中。可以根据需要扩展 `Course` 类,添加更多的属性和方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](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)