{ "title": "package exam;

import java.util.Scanner; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;

public class ExamManagementSystem {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Select Language:");
    System.out.println("1. 中文");
    System.out.println("2. English");
    int langChoice = scanner.nextInt();
    
    switch (langChoice) {
        case 1:
            System.out.println("欢迎使用考试管理系统!");
            System.out.println("请选择您是学生还是老师:");
            System.out.println("1. 学生");
            System.out.println("2. 老师");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("请选择您要进行的操作:");
                    System.out.println("1. 学生注册");
                    System.out.println("2. 学生登录");
                    System.out.println("3. 返回主页面");
                    int studentChoice = scanner.nextInt();

                    switch (studentChoice) {
                        case 1:
                            studentRegistration();
                            break;
                        case 2:
                            studentLogin();
                            break;
                        case 3:
                            System.out.println("返回主页面");
                            break;
                        default:
                            System.out.println("无效的选项!");
                            break;
                    }
                    break;
                case 2:
                    System.out.println("请输入姓名:");
                    String teacherName = scanner.nextLine();
                    System.out.println("请输入密码:");
                    String teacherPassword = scanner.nextLine();
                    System.out.println("是否登录?");
                    System.out.println("1. 是");
                    System.out.println("2. 不是");
                    int teacherLoginChoice = scanner.nextInt();

                    switch (teacherLoginChoice) {
                        case 1:
                            teacherLogin();
                            break;
                        case 2:
                            System.out.println("返回主页面");
                            break;
                        default:
                            System.out.println("无效的选项!");
                            break;
                    }
                    break;
                default:
                    System.out.println("无效的选项!");
                    break;
            }
            break;
        case 2:
            System.out.println("Welcome to Exam Management System!");
            System.out.println("Please choose if you are a student or a teacher:");
            System.out.println("1. Student");
            System.out.println("2. Teacher");
            int Choice = scanner.nextInt();

            switch (Choice) {
                case 1:
                    System.out.println("Please choose your option:");
                    System.out.println("1. Student Registration");
                    System.out.println("2. Student Login");
                    System.out.println("3. Return to Main Menu");
                    int studentChoice = scanner.nextInt();

                    switch (studentChoice) {
                        case 1:
                            studentRegistration();
                            break;
                        case 2:
                            studentLogin();
                            break;
                        case 3:
                            System.out.println("Returning to Main Menu");
                            break;
                        default:
                            System.out.println("Invalid option!");
                            break;
                    }
                    break;
                case 2:
                    System.out.println("Please enter your name:");
                    String teacherName = scanner.nextLine();
                    System.out.println("Please enter your password:");
                    String teacherPassword = scanner.nextLine();
                    System.out.println("Do you want to login?");
                    System.out.println("1. Yes");
                    System.out.println("2. No");
                    int teacherLoginChoice = scanner.nextInt();

                    switch (teacherLoginChoice) {
                        case 1:
                            teacherLogin();
                            break;
                        case 2:
                            System.out.println("Returning to Main Menu");
                            break;
                        default:
                            System.out.println("Invalid option!");
                            break;
                    }
                    break;
                default:
                    System.out.println("Invalid option!");
                    break;
            }
            break;
        default:
            System.out.println("Invalid option!");
            break;
    }
}

// 学生注册
public static void studentRegistration() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入学号:");
    int studentId = scanner.nextInt();
    System.out.println("请输入姓名:");
    String name = scanner.nextLine();
    System.out.println("请输入密码:");
    String password = scanner.nextLine();
    System.out.println("请再次输入密码:");
    String confirmPassword = scanner.nextLine();
    
    if (!password.equals(confirmPassword)) {
        System.out.println("两次输入的密码不一致,请重新注册!");
        return;
    }
    
    // 将学生信息插入到数据库中
    try (Connection conn = DatabaseConnector.getConnection()) {
        String sql = "INSERT INTO students (student_id, name, password) VALUES (?, ?, ?)";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setInt(1, studentId);
        statement.setString(2, name);
        statement.setString(3, password);
        statement.executeUpdate();
        System.out.println("注册成功!");
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("注册失败,请重新尝试!");
    }
}

// 学生登录
public static void studentLogin() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入学号:");
    int studentId = scanner.nextInt();
    System.out.println("请输入密码:");
    String password = scanner.next();
    
    // 查询数据库验证学生登录信息
    try (Connection conn = DatabaseConnector.getConnection()) {
        String sql = "SELECT * FROM students WHERE student_id = ? AND password = ?";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setInt(1, studentId);
        statement.setString(2, password);
        ResultSet result = statement.executeQuery();
        if (result.next()) {
            System.out.println("登录成功!");
            System.out.println("请选择您要进行的操作:");
            System.out.println("1. 开始考试");
            System.out.println("2. 查看成绩");
            System.out.println("3. 返回主页面");
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    studentStartExam();
                    break;
                case 2:
                    // 查看成绩页面逻辑
                    break;
                case 3:
                    System.out.println("返回主页面");
                    break;
                default:
                    System.out.println("无效的选项!");
                    break;
            }

        } else {
            System.out.println("学号或密码错误,请重新登录!");
            studentLogin(); // 返回登录页面
        }
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("登录失败,请重新尝试!");
    }
}

// 学生开始考试页面
public static void studentStartExam() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入您的学号:");
    int studentId = scanner.nextInt();
    System.out.println("请选择您要考试的学科:");
    System.out.println("1. Mathematics");
    System.out.println("2. English");
    System.out.println("3. Science");
    int subjectChoice = scanner.nextInt();
    
    String subject = "";
    int totalQuestions = 0;
    switch (subjectChoice) {
        case 1:
            subject = "Mathematics";
            totalQuestions = 10;
            break;
        case 2:
            subject = "English";
            totalQuestions = 10;
            break;
        case 3:
            subject = "Science";
            totalQuestions = 10;
            break;
        default:
            System.out.println("无效的选项!");
            return;
    }
    
    int score = 0;
    for (int i = 1; i <= totalQuestions; i++) {
        System.out.println("问题 " + i + ":");
        // 从数据库中获取题目
        try (Connection conn = DatabaseConnector.getConnection()) {
            String sql = "SELECT * FROM questions WHERE subject = ? ORDER BY RAND() LIMIT 1";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, subject);
            ResultSet result = statement.executeQuery();
            if (result.next()) {
                String question = result.getString("question");
                String optionA = result.getString("option_a");
                String optionB = result.getString("option_b");
                String optionC = result.getString("option_c");
                String optionD = result.getString("option_d");
                String answer = result.getString("answer");
                
                System.out.println(question);
                System.out.println("A. " + optionA);
                System.out.println("B. " + optionB);
                System.out.println("C. " + optionC);
                System.out.println("D. " + optionD);
                System.out.println("请选择您的答案:");
                String studentAnswer = scanner.next();
                
                if (studentAnswer.equalsIgnoreCase(answer)) {
                    score += 10;
                    System.out.println("回答正确!");
                } else {
                    System.out.println("回答错误!");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("获取题目失败!");
        }
        
        if (i < totalQuestions) {
            System.out.println("请选择您要进行的操作:");
            System.out.println("1. 下一题");
            System.out.println("2. 完成答题");
            int answerChoice = scanner.nextInt();
            
            if (answerChoice == 2) {
                break;
            }
        }
    }
    
    System.out.println("您的得分是:" + score);
    
    // 将学生的分数保存到数据库中
    try (Connection conn = DatabaseConnector.getConnection()) {
        String sql = "INSERT INTO exam_results (student_id,course_id,score) VALUES (?, ?, ?)";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setInt(1, studentId);
        statement.setString(2, subject);
        statement.setInt(3, score);
        statement.executeUpdate();
        System.out.println("分数已保存!");
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("保存分数失败!");
    }
}

// 教师登录
public static void teacherLogin() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("请选择您要进行的操作:");
    System.out.println("1. 查看问题");
    System.out.println("2. 添加问题");
    System.out.println("3. 删除问题");
    System.out.println("4. 更新问题");
    System.out.println("5. 返回主页面");
    int choice = scanner.nextInt();

    switch (choice) {
        case 1:
            viewQuestions();
            break;
        case 2:
            // 添加问题页面逻辑
            break;
        case 3:
            // 删除问题页面逻辑
            break;
        case 4:
            // 更新问题页面逻辑
            break;
        case 5:
            System.out.println("返回主页面");
            break;
        default:
            System.out.println("无效的选项!");
            break;
    }
}

// 查看问题页面逻辑
public static void viewQuestions() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Welcome Leader!");
    System.out.println("Please choose a course:");
    System.out.println("1. Java");
    System.out.println("2. MySQL");
    System.out.println("3. All courses");
    int courseChoice = scanner.nextInt();

    String course = "";
    if (courseChoice == 1) {
        course = "Java";
    } else if (courseChoice == 2) {
        course = "MySQL";
    }

    try (Connection conn = DatabaseConnector.getConnection()) {
        String sql = "SELECT * FROM questions";
        if (!course.equals("")) {
            sql += " WHERE course = ?";
        }
        PreparedStatement statement = conn.prepareStatement(sql);
        if (!course.equals("")) {
            statement.setString(1, course);
        }
        ResultSet result = statement.executeQuery();

        while (result.next()) {
            String queNo = result.getString("que_no");
            String queName = result.getString("que_name");
            String courseName = result.getString("course");
            String answer = result.getString("answer");

            System.out.println(courseName + " " + queNo + " " + queName);
            System.out.println("Answer: " + answer);
        }
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("Failed to retrieve questions!");
    }

    System.out.println("Do you want to go back to the Admin page now?");
    System.out.println("1. Yes");
    System.out.println("2. No");
    int backChoice = scanner.nextInt();

    if (backChoice == 1) {
        teacherLogin();
    } else {
        System.out.println("Returning to Main Menu");
    }
}

}

Java and MySQL Exam Management System - Online Exam Platform

原文地址: https://www.cveoy.top/t/topic/f0II 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录