【问题标题】:2D arrays, Strings, doubles and addition二维数组、字符串、双精度和加法
【发布时间】:2013-09-19 04:39:35
【问题描述】:

我的任务是创建一个程序,该程序将扫描一个包含学生详细信息和成绩的 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

最后是 StudentMarks 类:

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 中会更容易,但我不确定如何开始。 任何帮助将不胜感激!

【问题讨论】:

  • 你明白System.out.printSystem.out.println的区别吗?
  • 我进行了编辑,现在您觉得更有意义了吗?

标签: java arrays string text double


【解决方案1】:

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

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

println 将打印然后打印一个新行,将它们更改为 print 然后在第一个/最后一个添加新行字符或 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 的ArrayList 像这样ArrayList&lt;Student&gt; 然后你只需要在你的学生对象类中有一个toString() 方法,它会以你想要的格式打印一行。

【讨论】:

  • 我已经进行了编辑,如果这对您有帮助,那就太好了!干杯
  • 太棒了!这极大地帮助了我的输出,但是您将如何计算平均值?当数据应该是双精度时,它不是仍然是字符串吗?
  • 你能帮我吗?
  • 只有输出是一个字符串,你的数据仍然在数组中作为双精度数。所以你可以再次循环计算平均值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多