java学生选课系统

Java做一个学生选课系统

代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentCourseSystem {
    public static void main(String[] args) {
        List courses = new ArrayList<>();
        List selectedCourses = new ArrayList<>();
        
        courses.add("Java编程");
        courses.add("数据库管理");
        courses.add("数据结构");
        
        Scanner scanner = new Scanner(System.in);
        
        while (true) {
            System.out.println("1. 选课");
            System.out.println("2. 退课");
            System.out.println("3. 查询已选课程");
            System.out.println("4. 退出系统");
            System.out.print("请输入您的选择: ");
            
            int choice = scanner.nextInt();
            
            switch (choice) {
                case 1:
                    System.out.println("可选课程列表:");
                    for (int i = 0; i < courses.size(); i++) {
                        System.out.println((i + 1) + ". " + courses.get(i));
                    }
                    System.out.print("请输入要选择的课程编号:");
                    int selectCourseIndex = scanner.nextInt();
                    
                    if (selectCourseIndex >= 1 && selectCourseIndex <= courses.size()) {
                        String selectedCourse = courses.get(selectCourseIndex - 1);
                        if (selectedCourses.contains(selectedCourse)) {
                            System.out.println("已选择该课程,请勿重复选择");
                        } else {
                            selectedCourses.add(selectedCourse);
                            System.out.println("选课成功");
                        }
                    } else {
                        System.out.println("无效的课程编号");
                    }
                    break;
                case 2:
                    System.out.println("已选课程列表:");
                    for (int i = 0; i < selectedCourses.size(); i++) {
                        System.out.println((i + 1) + ". " + selectedCourses.get(i));
                    }
                    System.out.print("请输入要退选的课程编号:");
                    int removeCourseIndex = scanner.nextInt();
                    
                    if (removeCourseIndex >= 1 && removeCourseIndex <= selectedCourses.size()) {
                        selectedCourses.remove(removeCourseIndex - 1);
                        System.out.println("退课成功");
                    } else {
                        System.out.println("无效的课程编号");
                    }
                    break;
                case 3:
                    System.out.println("已选课程列表:");
                    for (int i = 0; i < selectedCourses.size(); i++) {
                        System.out.println((i + 1) + ". " + selectedCourses.get(i));
                    }
                    break;
                case 4:
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("无效的选择");
                    break;
            }
        }
    }
}

运行结果:

java学生选课系统_第1张图片

你可能感兴趣的:(java,开发语言)