我的任务是创建一个程序,扫描一个文本文件,其中包含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行,第一行是:
等10个随机创建的学生。我有3个班级,一个名为Topic Management的主班,一个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中会更容易但我不知道如何开始。 任何帮助将不胜感激!
答案 0 :(得分: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<Student>
的ArrayList,那么你只需要在你的学生对象类中有一个toString()
方法,它会以你想要的格式打印一行。