如何在java控制台中以表格形式显示arraylist

时间:2014-03-10 12:58:09

标签: java arraylist

我想以表格形式显示arraylist学生的数据。我有这个代码,但它只显示我在表格中输入的最后一组数据。我的代码出了什么问题?

public class gradesummary extends student{

    static ArrayList<student> studentList = new ArrayList<student>();

    @SuppressWarnings("resource")
    public static void main(String[] args){
        student student = new student();

        Scanner in = new Scanner(System.in);

        for (int x=0; x<=10; x++){
            System.out.print("Student Name: ");
            try {
                    student.name = in.nextLine();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 
            System.out.print("Section: ");
            try {
                    student.section = in.nextLine();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 
            System.out.print("Midterm Grade: ");
            try {
                    student.mgrade = in.nextDouble();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 
            System.out.print("Second Quarter Grade: ");
            try {
                    student.sqgrade = in.nextDouble();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 
            System.out.print("Final Exam Grade: ");
            try {
                    student.fegrade = in.nextDouble();
                } 
            catch (Exception e) 
                {
                    e.printStackTrace();
                } 

            student.fgrade = (student.mgrade*1/5)+(student.sqgrade*3/10)+(student.fegrade*9/20);
            studentList.add(student);
            in.nextLine();
        }
        Show(studentList);
    }

    private static void Show(ArrayList<student> studentList2) {
        new student();
        System.out.println("               Name           Section          Midterm Grade          Second Quarter Grade         Final Exam Grade         Final Grade     ");
        for (int i=0;i<=10;i++)
        {
            student student1 = studentList.get(i);
            System.out.println ("                      " +student1.name+ "          "+student1.section+"           "+student1.mgrade+"         "+student1.sqgrade+"        "+student1.fegrade+"       "+student1.fgrade+" " );
        }  
    }              
}

这是我的班级学生

public class student {

String name;
String section;
Double mgrade;
Double sqgrade;
Double fegrade;
Double fgrade;
}

How can I make the table data straight? more organize?

如何直接制作表格数据?更多组织?像这样: enter image description here

2 个答案:

答案 0 :(得分:1)

您已经将学生对象声明了一次,因此您将使用相同的学生对象来插入最后一个值。

尝试将学生的声明移到循环中,如下所示:

for (int x=0; x<=10; x++){
    student student = new student();
    //rest of your code
}

答案 1 :(得分:1)

您只创建一名学生,并且始终覆盖学生数据。在完成所有输入后,您必须创建一个新学生

    student.fgrade = (student.mgrade*1/5)+(student.sqgrade*3/10)+(student.fegrade*9/20);
    studentList.add(student);
    student = new student();
    in.nextLine();

同时从Show中删除新的student()。我还建议使用另一个变量名。