private static int studentId; public static void mainString args Scanner scanner = new ScannerSystemin; int langChoice = chooseLanguagescanner; int identityChoice = chooseIden
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 { // 用户名或密码错误 System.out.println("用户名或密码错误!"); } } 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("请输入密码:"); break; case 2: System.out.println("Please enter your password:"); break; default: return ""; } return scanner.next(); }
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 boolean isValidTeacher(String username, String password) { // 这里可以连接数据库,查询用户名和密码是否匹配 if (username.equals("teacher") && password.equals("123456")) { return true; } else { return false; } }
// 学生注册 public static void studentRegistration(int langChoice) { Scanner scanner = new Scanner(System.in); switch (langChoice) { case 1: System.out.println("请输入学号:"); break; case 2: System.out.println("Please enter your student ID:"); break; } int studentId = scanner.nextInt(); scanner.nextLine(); // 读取换行符 switch (langChoice) { case 1: System.out.println("请输入姓名:"); break; case 2: System.out.println("Please enter your name:"); break; } String name = scanner.nextLine(); switch (langChoice) { case 1: System.out.println("请输入密码:"); break; case 2: System.out.println("Please enter your password:"); break; } String password = scanner.nextLine();
// 将学生信息插入到数据库中
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();
switch (langChoice) {
case 1:
System.out.println("注册成功!");
System.out.println("请选择您要进行的操作:");
System.out.println("1. 继续注册");
System.out.println("2. 学生登录");
break;
case 2:
System.out.println("Registration successful!");
System.out.println("Please choose your option:");
System.out.println("1. Continue registration");
System.out.println("2. Student Login");
break;
}
int choice = scanner.nextInt();
switch (choice) {
case 1:
studentRegistration(langChoice);
break;
case 2:
studentLogin(langChoice);
break;
default:
switch (langChoice) {
case 1:
System.out.println("无效的选项!");
break;
case 2:
System.out.println("Invalid option!");
break;
}
break;
}
} catch (SQLException e) {
e.printStackTrace();
switch (langChoice) {
case 1:
System.out.println("注册失败,请重新尝试!");
break;
case 2:
System.out.println("Registration failed. Please try again!");
break;
}
}
原文地址: https://www.cveoy.top/t/topic/hiuF 著作权归作者所有。请勿转载和采集!