java代码:学生选课系统至少应包括以下信息: 学生信息:学号、姓名、性别、年级、密码 课程信息:课程编号、课程名称、已选人数、可选人数、授课教师。
时间: 2023-09-08 16:15:46 浏览: 151
javaweb学生信息选课管理系统源代码.zip
5星 · 资源好评率100%
好的,以下是一个简单的学生选课系统Java代码实现,包括学生信息和课程信息:
```java
import java.util.Scanner;
public class StudentCourseSystem {
static Scanner input = new Scanner(System.in);
static Student[] students = new Student[10];
static Course[] courses = new Course[5];
static int studentIndex = 0;
static int courseIndex = 0;
public static void main(String[] args) {
while (true) {
System.out.println("请选择您要进行的操作:");
System.out.println("1.学生注册 2.学生登录 3.查看课程信息 4.选课 5.退课 6.退出");
int choice = input.nextInt();
switch (choice) {
case 1:
register();
break;
case 2:
login();
break;
case 3:
showCourseInfo();
break;
case 4:
chooseCourse();
break;
case 5:
dropCourse();
break;
case 6:
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
// 学生注册
public static void register() {
System.out.println("请输入学生信息:");
System.out.print("学号:");
String id = input.next();
System.out.print("姓名:");
String name = input.next();
System.out.print("性别:");
String gender = input.next();
System.out.print("年级:");
String grade = input.next();
System.out.print("密码:");
String password = input.next();
students[studentIndex++] = new Student(id, name, gender, grade, password);
System.out.println("注册成功!");
}
// 学生登录
public static void login() {
System.out.print("请输入学号:");
String id = input.next();
System.out.print("请输入密码:");
String password = input.next();
for (int i = 0; i < studentIndex; i++) {
if (students[i].getId().equals(id) && students[i].getPassword().equals(password)) {
System.out.println("登录成功!");
return;
}
}
System.out.println("学号或密码错误!");
}
// 查看课程信息
public static void showCourseInfo() {
System.out.println("课程编号\t课程名称\t已选人数\t可选人数\t授课教师");
for (int i = 0; i < courseIndex; i++) {
Course course = courses[i];
System.out.println(course.getId() + "\t" + course.getName() + "\t" + course.getSelected() + "\t" + course.getLimit() + "\t" + course.getTeacher());
}
}
// 选课
public static void chooseCourse() {
System.out.print("请输入课程编号:");
String id = input.next();
for (int i = 0; i < courseIndex; i++) {
Course course = courses[i];
if (course.getId().equals(id)) {
if (course.getSelected() < course.getLimit()) {
course.setSelected(course.getSelected() + 1);
System.out.println("选课成功!");
} else {
System.out.println("该课程已选满!");
}
return;
}
}
System.out.println("课程编号错误!");
}
// 退课
public static void dropCourse() {
System.out.print("请输入课程编号:");
String id = input.next();
for (int i = 0; i < courseIndex; i++) {
Course course = courses[i];
if (course.getId().equals(id)) {
if (course.getSelected() > 0) {
course.setSelected(course.getSelected() - 1);
System.out.println("退课成功!");
} else {
System.out.println("您未选该课程!");
}
return;
}
}
System.out.println("课程编号错误!");
}
}
class Student {
private String id;
private String name;
private String gender;
private String grade;
private String password;
public Student(String id, String name, String gender, String grade, String password) {
this.id = id;
this.name = name;
this.gender = gender;
this.grade = grade;
this.password = password;
}
public String getId() {
return id;
}
public String getPassword() {
return password;
}
}
class Course {
private String id;
private String name;
private int selected;
private int limit;
private String teacher;
public Course(String id, String name, int limit, String teacher) {
this.id = id;
this.name = name;
this.limit = limit;
this.teacher = teacher;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getLimit() {
return limit;
}
public String getTeacher() {
return teacher;
}
}
```
该代码实现了学生注册、登录、查看课程信息、选课和退课等功能,其中学生和课程信息分别用Student和Course类表示,使用数组存储。
阅读全文