0

我的任务是创建一个程序,该程序将扫描一个包含学生详细信息和成绩的 4 个作业的文本文件,然后将其分解为字符串(详细信息)和双打(学生成绩),最后计算平均值每个学生的成绩,然后是每个作业的平均值。最终输出应如下所示(从我的作业表中获得):

Student Name     FAN       Part 1  Part 2 Part 3 Part 4 Mark   Grade
Adam Adamson     adam0001  85.4    79.8   82.4   86.1   82.77% HD
Bethany Bright   brig0001  89.7    85.6   84.2   82.9   84.92% DN
Cameron Carlson  carl0001  55.45   49.82  60.4   42.27  50.23% P
David Dawson     daws0001  72.6    78.49  80.2   65.88  74.46% CR
Evelyn Ellis     elli0001  50.2    35.88  48.41  58.37  46.57% FA
Frances Fitz     fitz0001  78.9    75.67  82.48  79.1   78.38% DN
Greg Gregson     greg0001  24.3    32.88  29.72  28.4   30.05% F
Harriett Hope    hope0001  52.2    58.93  61.5   63.44  60.12% P
Ivan Indigo      indi0001  88.4    91.23  90.05  92.46  91.08% HD
Jessica Jones    jone0001  82.33   89.74  81.3   84.85  85.84% HD
                  Average  67.948  67.804 70.066 68.377 68.44% CR
                                                StdDev 19.4441

在文本文件中有 10 行,第一行是:

亚当亚当森,adam0001,85.4,79.8,82.4,86.1

以此类推,随机创建 10 个学生。我有 3 个类,一个名为主题管理的主类,一个 StudentMarks 类(用于学生成绩)和一个 Student 类(用于名称和 FAN(adam0001 部分))。我创建了一个数组来存储所有称为标记的分数。我可以打印出姓名和 FAN,还可以使用 double score1 = double.parseDouble(); 将成绩分配给双打;等等每个分数都有它自己的方法,然后我在主类中调用它。

我遇到的麻烦是让每列结果打印在名称和风扇旁边。目前他们只是在下面的单列中打印出来。然后我需要计算平均值,我不知道该怎么做。任何帮助将不胜感激。到目前为止,这是我的程序:

这是我的主要课程 - 主题管理:

public class TopicManagement 
{

ArrayList part1 = new ArrayList(10);
ArrayList part2 = new ArrayList(10);
ArrayList part3 = new ArrayList(10);
ArrayList part4 = new ArrayList(10);

public static void main(String[] args) throws IOException 
{   

    System.out.println("Hello, Welcome to the Student Assesment Calculator");
    System.out.println("Student Name \t  FAN \t\tScore 1\tScore2\tScore 3\tScore 4\tTotal");

    Student student = new Student();
    StudentMarks studentMarks = new StudentMarks();

    for (int row = 0; row < nameFan.length; row++) 
    {
         System.out.println(nameFan[row][0] + "\t " + nameFan[row][1] + "\t ");
         //System.out.println("");
    }

    for (int col = 0; col < marks.length; col++)
    {
        double score1 = Double.parseDouble(marks[col][2]);
        double score2 = Double.parseDouble(marks[col][3]);
        double score3 = Double.parseDouble(marks[col][4]);
        double score4 = Double.parseDouble(marks[col][5]);

        part1.add(score1);
        part2.add(score2);
        part3.add(score3);
        part4.add(score4);

        System.out.println(part1.get(col) + "\t" + part2.get(col) + "\t" + 
                           part3.get(col) + "\t" + part4.get(col) + "\t");


    }

}//end of method
}//end of class

学生班:

public class Student 
{           //ROW, COL
    String[][] nameFan = new String[10][6];
            //this method outputs the name and fan
public void student() throws IOException 
{   
    Scanner scan = new Scanner (new File ("TestResults.txt"));


    for (int row = 0, col; row < nameFan.length; row++)
    {
        Scanner lineRead = new Scanner(scan.nextLine());
        lineRead.useDelimiter(",");

        col = 0; // Starting at column 0 for each row

        while (lineRead.hasNext()) //Check for next
        {
            nameFan[row][col]=lineRead.next();

            col++; // Move on to the next column   
        }
    }
}//end of nameFan method
}//end of class

最后是 StudentsMarks 课程:

public class StudentMarks 
{                 
        //ROW, COL
String[][] marks = new String[10][6];

        //was going to call the method 'student' but would have been confusing with the student class
public String[][] studentMarks() throws IOException 
{  
    Scanner scan = new Scanner (new File ("TestResults.txt"));


    for (int row = 0, col; row < marks.length; row++)
    {
        Scanner lineRead = new Scanner(scan.nextLine());
        lineRead.useDelimiter(",");

        col = 0; //Starting at column 0 for each row

        while (lineRead.hasNext()) //Check for next
        {
            marks[row][col]=lineRead.next();

            col++; // Move on to the next column
        }
    }

    return marks;
}   
}

现在的输出如下所示:

Hello, Welcome to the Student Assesment Calculator
Student Name      FAN       Score 1 Score2  Score 3 Score 4 Total
Adam Adamson     adam0001    
Bethany Bright   brig0001    
Cameron Carlson  carl0001    
David Dawson     daws0001    
Evelyn Ellis     elli0001    
Frances Fitz     fitz0001    
Greg Gregson     greg0001    
Harriett Hope    hope0001    
Ivan Indigo      indi0001    
Jessica Jones    jone0001    
85.4    79.8    82.4    86.1    
89.7    85.6    84.2    82.9    
55.45   49.82   60.4    42.27   
72.6    78.49   80.2    65.88   
50.2    35.88   48.41   58.37   
78.9    75.67   82.48   79.1    
24.3    32.88   29.72   28.4    
52.2    58.93   61.5    63.44   
88.4    91.23   90.05   92.46   
82.33   89.74   81.3    84.85

好的,所以我现在所做的不再是调用 student 和 studentMarks 类中的方法的输出。我正在获取数组并将它们放入 ArrayLists(part1、part2 等),然后打印这些数组列表。我仍然遇到输出格式的问题。我不能让他们在同一条线上。我理解你所说的 println 和 print 行是什么意思,但我需要循环中的那些,否则他们的学生姓名和粉丝等只会在一行上打印所有内容,不是吗?

而且我不确定现在如何计算平均值(或标记为百分比,取决于您如何阅读它)我希望现在它们在 ArrayList 中会更容易,但我不确定如何开始. 任何帮助将不胜感激!

4

1 回答 1

1

在您的评分方法中,您有一个println

System.out.print(score4);
System.out.println("\t4");  //Notice this is a println

println将打印然后打印一个新行,将这些更改为 aprint然后在第一个/最后一个添加新行字符或println

编辑

您现在将分数放入 ArrayList 中,然后在同一循环中打印 ArrayList。这是没有意义的

for (int col = 0; col < marks.length; col++)
{
    double score1 = Double.parseDouble(marks[col][2]);
    double score2 = Double.parseDouble(marks[col][3]);
    double score3 = Double.parseDouble(marks[col][4]);
    double score4 = Double.parseDouble(marks[col][5]);

    part1.add(score1);
    part2.add(score2);
    part3.add(score3);
    part4.add(score4);

    System.out.println(part1.get(col) + "\t" + part2.get(col) + "\t" + 
                       part3.get(col) + "\t" + part4.get(col) + "\t");
}

可以用这个来完成

for (int col = 0; col < marks.length; col++)
{
    System.out.println(marks[col][2] + "\t" + marks[col][3] + "\t" + 
                       marks[col][4] + "\t" + marks[col][5] + "\t");
}

请注意您仍然有两个循环并且仍然为循环的每次迭代打印一行,只需执行此操作即可获得所需的输出,因为所有数组的长度都相同

for (int i = 0; i < marks.length; i++)
{
    System.out.println(nameFan[i][0] + "\t " + nameFan[i][1] + "\t
                       marks[i][2] + "\t" + marks[i][3] + "\t" + 
                       marks[i][4] + "\t" + marks[i][5] + "\t");
}

理想情况下,您的Student班级应该只保存一个学生的信息,然后您就不需要数组。同样,您的 StudentMarks 课程应该只有一个学生的分数,然后部分学生将拥有一个StudentMarks属性。然后你可以有一个Students这样的 ArrayListArrayList<Student>然后你只需要在你的学生对象类中有一个toString()方法,它会以你想要的格式打印一行。

于 2013-09-19T04:51:55.480 回答