使用System.out.printf和数组显示成绩表

时间:2014-11-30 03:21:18

标签: java arrays

我正在努力学习我的课程,我正在寻求帮助。我应该能够输出一个包含我的学生姓名,所有考试成绩,测验成绩,家庭作业成绩和最终平均成绩的表格。我在确定如何使用System.out.printf正确格式化表时遇到了一些困难。现在我遇到了一个问题,我不明白如何循环我的阵列,所以他们不断打印出多达200个学生的名字和成绩。

我尝试通过做3个单独的学生成绩来解决这个问题,但是我遇到的问题是它说我的所有考试,测验和hw数组都无法解析为变量。这让我感到困惑,因为我已经将它们从一个字符串更改为一个双字符串。如果它有帮助,那么我用来尝试将输入切换为双精度的代码。

// declare Double[] exams using length numExams
                    Double[] exams = new Double[numExams];
                    // iterates through Array exams and adds values from allInfo
                    for (int k = 0; k < exams.length; k++) {
                        exams[k] = Double.parseDouble(allInfo.get(1 + k)
                                .substring(1));

                        // prints out (delete this when you finish program)
                        //System.out.print(exams[k] + "\t");
                    }

                    //System.out.println(" ");

                    // declare Double[] quizzes using length numQuizzes
                    Double[] quizzes = new Double[numQuizzes];
                    // iterates through Array quizzes and adds values from
                    // allInfo
                    for (int k = 0; k < quizzes.length; k++) {
                        quizzes[k] = Double.parseDouble(allInfo.get(
                                1 + numExams + k).substring(1));

                        // prints out (delete this when you finish program)
                        //System.out.print(quizzes[k] + "\t");
                    }

                    //System.out.println(" ");

                    // declare Double[] HW using length numHW
                    Double[] hw = new Double[numHW];
                    // iterates through Array hw and adds values from allInfo
                    for (int k = 0; k < hw.length; k++) {
                        hw[k] = Double.parseDouble(allInfo.get(
                                1 + numExams + numQuizzes + k).substring(1));

希望这能帮助您理解我的思路。这是我列出的代码,并试图弄清楚。

if (choice == 2) {
            System.out.println("Display student grades & statistics");

            //Formatting for the heading of my grade table
            System.out.printf("%-10s","Name");
            System.out.printf("%-5s","Exam");
            System.out.printf("%-5s","Exam");
            System.out.printf("%-5s","Exam");
            System.out.printf("%-5s","Quiz");
            System.out.printf("%-5s","Quiz");
            System.out.printf("%-5s","Quiz");
            System.out.printf("%-7s","HWork");
            System.out.printf("%-7s","HWork");
            System.out.printf("%-7s","HWork");
            System.out.printf("%-5s","Grade\n");

            System.out.printf("%-10s",studentNames[0]);
            System.out.printf("%-5.1f",exams[0]);
            System.out.printf("%-5.1f",exams[1]);
            System.out.printf("%-5.1f",exams[2]);
            System.out.printf("%-5.1f",quizzes[0]);
            System.out.printf("%-5.1f",quizzes[1]);
            System.out.printf("%-5.1f",quizzes[2]);
            System.out.printf("%-7.1f",hw[0]);
            System.out.printf("%-7.1f",hw[1]);
            System.out.printf("%-7.1f",hw[2]);
            System.out.printf("%-5.1f",gradeAverage[0] + "\n");

            System.out.printf("%-10s",studentNames[1]);
            System.out.printf("%-5.1f",exams[3]);
            System.out.printf("%-5.1f",exams[4]);
            System.out.printf("%-5.1f",exams[5]);
            System.out.printf("%-5.1f",quizzes[3]);
            System.out.printf("%-5.1f",quizzes[4]);
            System.out.printf("%-5.1f",quizzes[5]);
            System.out.printf("%-7.1f",hw[3]);
            System.out.printf("%-7.1f",hw[4]);
            System.out.printf("%-7.1f",hw[5]);
            System.out.printf("%-5.1f",gradeAverage[1] + "\n");

            System.out.printf("%-10s",studentNames[2]);
            System.out.printf("%-5.1f",exams[6]);
            System.out.printf("%-5.1f",exams[7]);
            System.out.printf("%-5.1f",exams[8]);
            System.out.printf("%-5.1f",quizzes[6]);
            System.out.printf("%-5.1f",quizzes[7]);
            System.out.printf("%-5.1f",quizzes[8]);
            System.out.printf("%-7.1f",hw[6]);
            System.out.printf("%-7.1f",hw[7]);
            System.out.printf("%-7.1f",hw[8]);
            System.out.printf("%-5.1f",gradeAverage[2] + "\n");


                }

2 个答案:

答案 0 :(得分:0)

最好的方法是使用POJA。创建一个Sudent的POJO,其中包含您需要的所有字段。然后创建一个Student对象的arrary并遍历它。但是对于您当前的代码,请使用以下解决方案。

维护所有4个阵列的索引(即学生姓名,所有考试成绩,测验成绩,家庭作业成绩)。

int studentNameIndex = 0;
int examGradeIndex = 0;
int quizGradeIndex = 0;
int homeworkGradeIndex = 0;
int gradeAverageIndex = 0;

System.out.println("Display student grades & statistics");

//Formatting for the heading of my grade table
System.out.printf("%-10s","Name");
System.out.printf("%-5s","Exam");
System.out.printf("%-5s","Exam");
System.out.printf("%-5s","Exam");
System.out.printf("%-5s","Quiz");
System.out.printf("%-5s","Quiz");
System.out.printf("%-5s","Quiz");
System.out.printf("%-7s","HWork");
System.out.printf("%-7s","HWork");
System.out.printf("%-7s","HWork");
System.out.printf("%-5s","Grade\n");

for(int index = 0; index < studentNames.length; i++) {
    System.out.printf("%-10s",studentNames[studentNameIndex]);
    System.out.printf("%-5.1f",exams[examGradeIndex]);
    System.out.printf("%-5.1f",exams[examGradeIndex+1]);
    System.out.printf("%-5.1f",exams[examGradeIndex+2]);
    System.out.printf("%-5.1f",quizzes[quizGradeIndex]);
    System.out.printf("%-5.1f",quizzes[quizGradeIndex+1]);
    System.out.printf("%-5.1f",quizzes[quizGradeIndex+2]);
    System.out.printf("%-7.1f",hw[homeworkGradeIndex]);
    System.out.printf("%-7.1f",hw[homeworkGradeIndex+1]);
    System.out.printf("%-7.1f",hw[homeworkGradeIndex+2]);
    System.out.printf("%-5.1f",gradeAverage[gradeAverageIndex] + "\n");

    studentNameIndex++;
    examGradeIndex+=3;
    quizGradeIndex+=3;
    homeworkGradeIndex+=3;
    gradeAverageIndex++;
}

答案 1 :(得分:0)

问题是行中的"\n"

System.out.printf("%-5.1f",gradeAverage[2] + "\n");

替换为

System.out.printf("%-5.1f \n",gradeAverage[1]);

或使用此

System.out.printf("%-5.1f",gradeAverage[2]);
System.out.printf("\n");