将package shiyan3; public class Test { public static void main(String[] args) { try { Student student = new Student("Alice", "123456", "Computer Science"); System.out.println("Student name: " + student.getUsername()); System.out.println("Student password: " + student.getPassword()); System.out.println("Student major: " + student.getMajor()); Teacher teacher = new Teacher("Bob", "123456", "Mathematics"); System.out.println("Teacher name: " + teacher.getUsername()); System.out.println("Teacher password: " + teacher.getPassword()); System.out.println("Teacher department: " + teacher.getDepartment()); Admin admin = new Admin("Charlie", "123456", "Director"); System.out.println("Admin name: " + admin.getUsername()); System.out.println("Admin password: " + admin.getPassword()); System.out.println("Admin position: " + admin.getPosition()); } catch (Exception e) { System.out.println("An error occurred: " + e.getMessage()); } } }修改这段代码,使他有键盘输入的值
时间: 2023-11-27 12:52:26 浏览: 77
shiyan7.class
以下是修改后的代码,使用了Scanner类来获取键盘输入的值:
```java
package shiyan3;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter student name: ");
String studentName = scanner.nextLine();
System.out.print("Enter student password: ");
String studentPassword = scanner.nextLine();
System.out.print("Enter student major: ");
String studentMajor = scanner.nextLine();
Student student = new Student(studentName, studentPassword, studentMajor);
System.out.println("Student name: " + student.getUsername());
System.out.println("Student password: " + student.getPassword());
System.out.println("Student major: " + student.getMajor());
System.out.print("\nEnter teacher name: ");
String teacherName = scanner.nextLine();
System.out.print("Enter teacher password: ");
String teacherPassword = scanner.nextLine();
System.out.print("Enter teacher department: ");
String teacherDepartment = scanner.nextLine();
Teacher teacher = new Teacher(teacherName, teacherPassword, teacherDepartment);
System.out.println("Teacher name: " + teacher.getUsername());
System.out.println("Teacher password: " + teacher.getPassword());
System.out.println("Teacher department: " + teacher.getDepartment());
System.out.print("\nEnter admin name: ");
String adminName = scanner.nextLine();
System.out.print("Enter admin password: ");
String adminPassword = scanner.nextLine();
System.out.print("Enter admin position: ");
String adminPosition = scanner.nextLine();
Admin admin = new Admin(adminName, adminPassword, adminPosition);
System.out.println("Admin name: " + admin.getUsername());
System.out.println("Admin password: " + admin.getPassword());
System.out.println("Admin position: " + admin.getPosition());
scanner.close();
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
```
阅读全文