以下是修改后的代码:

import java.util.Scanner;

public class ExamManagementSystem {

private static int studentId;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int langChoice = chooseLanguage(scanner);
    int identityChoice = chooseIdentity(scanner, langChoice);
    if (identityChoice == 1) {
        // 学生登录逻辑
    } else if (identityChoice == 2) {
        // 老师登录逻辑
        String username = enterUsername(scanner, langChoice);
        String password = enterPassword(scanner, langChoice);
        // 验证用户名和密码是否正确
        if (isValidTeacher(username, password)) {
            String choice = confirmLogin(scanner, langChoice);
            if (choice.equalsIgnoreCase("A")) {
                // 老师登录成功
                System.out.println("老师登录成功");
                boolean isRunning = true;
                while (isRunning) {
                    int operationChoice = chooseOperation(scanner, langChoice);
                    switch (operationChoice) {
                        case 1:
                            // 查看问题
                            System.out.println("查看问题");
                            break;
                        case 2:
                            // 添加问题
                            System.out.println("添加问题");
                            break;
                        case 3:
                            // 删除问题
                            System.out.println("删除问题");
                            break;
                        case 4:
                            // 更新问题
                            System.out.println("更新问题");
                            break;
                        case 5:
                            // 退出
                            isRunning = false;
                            break;
                        default:
                            invalidOption(langChoice);
                            break;
                    }
                }
            } else if (choice.equalsIgnoreCase("B")) {
                // 取消登录
                System.out.println("取消登录");
            } else if (choice.equalsIgnoreCase("C")) {
                // 返回首页
                System.out.println("返回首页");
            } else {
                invalidOption(langChoice);
            }
        } else {
            invalidUsernameOrPassword(langChoice);
        }
    } else {
        invalidOption(langChoice);
    }
}

private static int chooseLanguage(Scanner scanner) {
    System.out.println("请选择语言:");
    System.out.println("1. 中文");
    System.out.println("2. English");
    return scanner.nextInt();
}

private static int chooseIdentity(Scanner scanner, int langChoice) {
    switch (langChoice) {
        case 1:
            System.out.println("请选择您的身份:");
            System.out.println("1. 学生");
            System.out.println("2. 老师");
            break;
        case 2:
            System.out.println("Please choose your identity:");
            System.out.println("1. Student");
            System.out.println("2. Teacher");
            break;
    }
    return scanner.nextInt();
}

private static String enterUsername(Scanner scanner, int langChoice) {
    switch (langChoice) {
        case 1:
            System.out.println("请输入用户名:");
            break;
        case 2:
            System.out.println("Please enter your username:");
            break;
    }
    return scanner.next();
}

private static String enterPassword(Scanner scanner, int langChoice) {
    switch (langChoice) {
        case 1:
            System.out.println("请输入密码:");
            return scanner.next();
        case 2:
            System.out.println("Please enter your password:");
            return scanner.next();
        default:
            return "";
    }
}

private static String confirmLogin(Scanner scanner, int langChoice) {
    switch (langChoice) {
        case 1:
            System.out.println("你确定现在要登录吗?");
            System.out.println("A. 是");
            System.out.println("B. 否");
            System.out.println("C. 返回首页");
            break;
        case 2:
            System.out.println("Are you sure you want to log in now?");
            System.out.println("A. Yes");
            System.out.println("B. No");
            System.out.println("C. Back to the home page");
            break;
    }
    return scanner.next();
}

private static int chooseOperation(Scanner scanner, int langChoice) {
    switch (langChoice) {
        case 1:
            System.out.println("请选择您要进行的操作:");
            System.out.println("1. 查看问题");
            System.out.println("2. 添加问题");
            System.out.println("3. 删除问题");
            System.out.println("4. 更新问题");
            System.out.println("5. 退出");
            break;
        case 2:
            System.out.println("Please choose what you want to do:");
            System.out.println("1. View questions");
            System.out.println("2. Add question");
            System.out.println("3. Delete question");
            System.out.println("4. Update question");
            System.out.println("5. Exit");
            break;
    }
    return scanner.nextInt();
}

private static void invalidOption(int langChoice) {
    switch (langChoice) {
        case 1:
            System.out.println("无效的选项!");
            break;
        case 2:
            System.out.println("Invalid option!");
            break;
    }
}

private static void invalidUsernameOrPassword(int langChoice) {
    switch (langChoice) {
        case 1:
            System.out.println("用户名或密码错误!");
            break;
        case 2:
            System.out.println("Invalid username or password!");
            break;
    }
}

// 模拟老师用户名和密码是否匹配的方法
private static boolean isValidTeacher(String username, String password) {
    // 这里可以连接数据库,查询用户名和密码是否匹配
    if (username.equals("teacher") && password.equals("123456")) {
        return true;
    } else {
        return false;
    }
}
public class ExamManagementSystem private static int studentId; public static void mainString args Scanner scanner = new ScannerSystemin; Systemoutprintln请选择语言:; Systemout

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

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