如何在java控制台中以表格形式显示arraylist
我想以表格形式显示arraylist学生的数据。我有这段代码,但它只显示我在表中输入的最后一组数据。我的代码有什么问题如何在java控制台中以表格形式显示arraylist,java,arraylist,Java,Arraylist,我想以表格形式显示arraylist学生的数据。我有这段代码,但它只显示我在表中输入的最后一组数据。我的代码有什么问题 public class gradesummary extends student{ static ArrayList<student> studentList = new ArrayList<student>(); @SuppressWarnings("resource") public static void main(S
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+" " );
}
}
}
如何使表格数据更直观?更有组织性?这样地:
您只声明了一次学生对象,因此它将采用最后插入的值,因为您使用的是同一个学生对象 尝试将student声明移动到循环内部,如下所示:
for (int x=0; x<=10; x++){
student student = new student();
//rest of your code
}
for(int x=0;x您只创建一个学生,并且始终覆盖该学生数据。您必须在获得所有输入后创建一个新学生
student.fgrade = (student.mgrade*1/5)+(student.sqgrade*3/10)+(student.fegrade*9/20);
studentList.add(student);
student = new student();
in.nextLine();
同时从Show中删除new student()。我还建议使用另一个变量名。您必须动态计算字符串的大小,在末尾添加缺少的空格,以将字符串填充到最大长度,然后打印出来
student.fgrade = (student.mgrade*1/5)+(student.sqgrade*3/10)+(student.fegrade*9/20);
studentList.add(student);
student = new student();
in.nextLine();