簡體   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