gpt4 book ai didi

Java无法获取数组列表来打印?

转载 作者:行者123 更新时间:2023-12-01 07:54:00 25 4
gpt4 key购买 nike

所以我有 3 个类,其中一个运行所有代码。一个学生类、一个研究生类和一个本科生类,以及一个运行代码的名为实验 4 的类。该代码询问有多少名研究生和多少名本科生,然后要求用户输入该数量的所有信息。输入所有信息后,本科生或研究生对象将添加到学生数组列表中。添加完所有学生后,打印所有信息。但是,当我输入所有信息时,数组不打印,程序终止。如何打印数组?

代码:

实验 4:创建学生对象、数组列表,将它们添加到数组中,并打印添加到数组列表中的对象的所有信息

import java.util.ArrayList;
import java.util.Scanner;

public class Lab04 {

public static void main(String[] args) {
ArrayList <Student> studentList = new ArrayList <Student>();

Scanner s = new Scanner(System.in);
System.out.println("How many Graduate Students do you want to store?");
int numOfStudents = s.nextInt();
s.nextLine();
for (int i = 0; i < numOfStudents; i++) {
System.out.println("What is the student's name?");
String name = s.nextLine();
System.out.println("What is the student's GPA?");
double GPA = s.nextDouble();
s.nextLine();
System.out.println("What is the student's ID?");
String ID = s.nextLine();
System.out.println("What is the student's High School?");
String highSchool = s.nextLine();
System.out.println("What is the student's graduate major?");
String gradMajor = s.nextLine();
System.out.println("What is the student's undergraduate major?");
String underMajor = s.nextLine();
System.out.println("What is the student's undergradute school? ");
String underSchool = s.nextLine();
GraduateStudent gradStu = new GraduateStudent(name, GPA, ID, highSchool, gradMajor, underMajor,
underSchool);
studentList.add(gradStu);
}
System.out.println("How many UnderGraduate students are there?");
int numOfUnder = s.nextInt();
s.nextLine();
for (int j = 0; j < numOfUnder; j++) {
System.out.println("What is the student's name?");
String name = s.nextLine();
System.out.println("What is the student's GPA?");
double GPA = s.nextDouble();
s.nextLine();
System.out.println("What is the student's ID?");
String ID = s.nextLine();
System.out.println("What is the student's High School?");
String highSchool = s.nextLine();
System.out.println("What is the student's major?");
String major = s.nextLine();
System.out.println("What the student's minor?");
String minor = s.nextLine();
UnderGraduate UnderGrad = new UnderGraduate(name, GPA, ID, highSchool, major, minor);
studentList.add(UnderGrad);
}
for (int j = 0; j < studentList.size(); j++) {
(studentList.get(j)).getStudentInformation();
}
}

}

学生类(class):

public class Student {

private String name;
private double gpa;
private String id;
private String highSchool;
//private String major;
//private String minor;

public Student() {
name = "";
gpa = 0.0;
id = "";
highSchool = "";
//major = "";
//minor = "";
}
public Student(String name, double gpa, String id, String highSchool){
this.name = name;
this.gpa = gpa;
this.id = id;
this.highSchool = highSchool;

}



public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getGpa() {
return gpa;
}

public void setGpa(double gpa) {
this.gpa = gpa;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getHighSchool() {
return highSchool;
}

public void setHighSchool(String highSchool) {
this.highSchool = highSchool;
}


public String getStudentInformation() {
String result = "Name: " + name + " GPA: " + gpa + " ID: " + id
+ " High School: " + highSchool;
return result;
}

}

研究生类(class):

public class GraduateStudent extends Student {

private String gradMajor;
private String underMajor;
private String underSchool;

public GraduateStudent(String name, double gpa, String id, String highSchool,
String gradMajor, String underMajor, String underSchool) {
super(name, gpa, id, highSchool);
this.gradMajor = gradMajor;
this.underMajor = underMajor;
this.underSchool = underSchool;
}
public String getGradMajor() {
return gradMajor;
}

public void setGradMajor(String gradMajor) {
this.gradMajor = gradMajor;
}

public String getUnderMajor() {
return underMajor;
}

public void setUnderMajor(String underMajor) {
this.underMajor = underMajor;
}
public String getUnderSchool() {
return underSchool;
}

public void setUnderSchool(String underSchool) {
this.underSchool = underSchool;
}

@Override
public String getStudentInformation() {
String result = super.getStudentInformation()+
"Graduate Major: " + gradMajor + "Undergraduate Major: " + underMajor +
"Undergraduate School: " + underSchool;
return result;
}
}

最佳答案

因为你没有打印任何东西。改变

for (int j = 0; j < studentList.size(); j++) {
(studentList.get(j)).getStudentInformation();
}

for (int j = 0; j < studentList.size(); j++) {
System.out.println((studentList.get(j)).getStudentInformation());
}

关于Java无法获取数组列表来打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32618173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com