以下是用 Java 实现的代码示例: ```java import java.util.Scanner; public class StudentQuery { // 定义学生类 static class Student { String id; String name; int age; String courses; Student(String id, String name, int age, String courses) { this.id = id; this.name = name; this.age = age; this.courses = courses; } } // 查询学生信息的函数 public static Student queryStudent(String[] studentIds, Student[] students, String inputId) { for (int i = 0; i < studentIds.length; i++) { if (studentIds[i].equals(inputId)) { return students[i]; } } return null; } // 主函数 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 假设的学生信息数组 String[] studentIds = {"001", "002", "003"}; Student[] students = { new Student("001", "张三", 18, "数学,语文"), new Student("002", "李四", 19, "英语,物理"), new Student("003", "王五", 20, "化学,生物") }; System.out.print("请输入学号: "); String inputId = scanner.nextLine(); // 验证输入的学号是否有效 if (inputId == null || inputId.isEmpty()) { System.out.println("输入的学号无效,请重新输入。"); return; } Student student = queryStudent(studentIds, students, inputId); if (student!= null) { System.out.println("姓名: " + student.name); System.out.println("年龄: " + student.age); System.out.println("所选课程: " + student.courses); } else { System.out.println("学号无效,未找到对应的学生信息。"); } } } ``` 额外需要注意的逻辑点: 1. 对于学生信息的存储和管理,可以考虑使用数据库来提高效率和数据的安全性。 2. 输入的学号格式验证,确保符合预期的格式要求。 3. 对查询函数的性能优化,例如使用合适的数据结构(如哈希表)来提高查询速度。 [2024-12-17 14:10:19 | AI写代码神器 | 562点数解答]