我需要打印一个数组,但它只打印我输入的最后一个信息
这是打印代码
for(int i=0; i<undergrad.length;i++){
if(undergrad[i]!=null){
System.out.println("Student Name : "+undergrad[i].getName()+"\n"
+ "Full Address : "+undergrad[i].getAddress()+"\n"
+ "Mobile No :"+undergrad[i].getPhone()+"\n"
+ "Number of Tests : "+undergrad[i].getTests()+"\n"
+ "GBA : "+undergrad[i].GBA()+"\n"
+ "Status : "+undergrad[i].computeGrade()+"\n"
+ "Internship : "+undergrad[i].getInternship()+"\n"
+ "Project Title : "+undergrad[i].getProject_Title()+"\n"
+ "Project Area : "+undergrad[i].getProject_Area()+"\n"
+ "Project Score : "+undergrad[i].computeProjectGrade()+"\n"
+ "****************************************\n");
}
}
对象数组的大小为 10,但我可能会将信息放入少于 10 个(例如 4 个)中,当我打印时,我需要显示所有且只有我输入的 4 个
这就是添加部分
for(int i=0; i<undergrad.length; i++){
undergrad[i]=new Undergraduate();
System.out.println("Enter Student Name: ");
undergrad[i].setName(in.nextLine());
System.out.println("Full Address : ");
undergrad[i].setAddress(in.nextLine());
System.out.println("Mobile No : ");
undergrad[i].setPhone(in.nextLong());
System.out.println("Number of test : ");
undergrad[i].setTests(in.nextInt());
tests=undergrad[i].getTests();
for(int j=0; j<tests; j++){
System.out.println("Test "+(j+1)+":");
undergrad[i].setScores(in.nextInt(), j);
}
System.out.println("Internship: ");
undergrad[i].setInternship(in.nextBoolean());
in.nextLine();
System.out.println("Project title: ");
undergrad[i].setProject_Title(in.nextLine());
System.out.println("Project Area: ");
undergrad[i].setProject_Area(in.nextLine());
System.out.println("Supervisor Grade: ");
undergrad[i].setGrade(in.nextInt(), 0);
System.out.println("Committee Grade: ");
undergrad[i].setGrade(in.nextInt(), 1);
System.out.println("\n");
break;
}
我需要休息一下,因为在输入有关一个学生的详细信息后,应询问用户他想要执行的操作(例如\为另一位学生编写详细信息或打印有关已写学生的详细信息的操作)