以下是一个使用 Java 实现的示例代码,用于按照给定的类顺序输入数据: ```java import java.util.Scanner; class Student { int number; String name; double englishScore; double mathScore; double computerScore; } class StudentXW extends Student { String responsibility; } class StudentBZ extends Student { String responsibility; } public class DataInputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Student[] students = new Student[3]; // 输入第一个 Student 类的数据 String line1 = scanner.nextLine(); String[] parts1 = line1.split(","); students[0] = new Student(); students[0].number = Integer.parseInt(parts1[0]); students[0].name = parts1[1]; students[0].englishScore = Double.parseDouble(parts1[2]); students[0].mathScore = Double.parseDouble(parts1[3]); students[0].computerScore = Double.parseDouble(parts1[4]); // 输入第二个 Student 类的数据 String line2 = scanner.nextLine(); String[] parts2 = line2.split(","); students[1] = new StudentXW(); ((StudentXW)students[1]).number = Integer.parseInt(parts2[0]); ((StudentXW)students[1]).name = parts2[1]; ((StudentXW)students[1]).englishScore = Double.parseDouble(parts2[2]); ((StudentXW)students[1]).mathScore = Double.parseDouble(parts2[3]); ((StudentXW)students[1]).computerScore = Double.parseDouble(parts2[4]); ((StudentXW)students[1]).responsibility = "XWResponsibility"; // 假设的责任信息 // 输入第三个 Student 类的数据 String line3 = scanner.nextLine(); String[] parts3 = line3.split(","); students[2] = new StudentBZ(); ((StudentBZ)students[2]).number = Integer.parseInt(parts3[0]); ((StudentBZ)students[2]).name = parts3[1]; ((StudentBZ)students[2]).englishScore = Double.parseDouble(parts3[2]); ((StudentBZ)students[2]).mathScore = Double.parseDouble(parts3[3]); ((StudentBZ)students[2]).computerScore = Double.parseDouble(parts3[4]); ((StudentBZ)students[2]).responsibility = "BZResponsibility"; // 假设的责任信息 scanner.close(); } } ``` 在上述代码中,我们使用`Scanner`类从控制台读取输入的行数据,然后通过字符串分割和类型转换将数据赋值给相应的对象属性。这里只是简单地处理了输入数据,实际应用中可能需要添加更多的错误处理和逻辑。 [2024-10-20 10:07:05 | AI写代码神器 | 684点数解答]