import java.util.Scanner;
public class StudentMap extends Student{
public static void main(String[] args)
{
Student[] studentClass = new Student[2];
Scanner s1 = new Scanner(System.in);
for(int i=1;i<=studentClass.length;i++)
{
System.out.println("Enter name for "+i+"Student");
String studentName = s1.next();
System.out.println("Enter email for "+i+"Student");
String email = s1.next();
System.out.println("Enter phone number for "+i+"Student");
Integer num = s1.nextInt();
studentClass[i] = new Student();
studentClass[i].setName(studentName);
studentClass[i].setEmail(email);
studentClass[i].setPhoneNumber(num);
}
s1.close();
for(Student stu : studentClass)
{
System.out.println(stu.name);
System.out.println(stu.email);
System.out.println(stu.phoneNumber);
}
}
}
执行此代码时,它会出现OutOfBoundIndex错误。 我已创建了Student类,其中包含name,email和phoneNumber的所有set方法。