繁体   English   中英

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

[英]Java can't get array list to print out?

所以我有3个类,其中一个可以运行所有代码。 一个学生班,一个研究生班和一个本科班,以及一个运行代码的名为Lab 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();
    }
}

}

学生班:

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;
}

}

研究生班:

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());
}

当您询问getStudentInformation时,它返回一个String,您现在要对System.out.println(...)这个String对象进行操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM