我是一个初学者在java,我很努力让这个代码工作。我正在尝试读取CSV文件,然后使用该数据计算平均值,然后返回最高平均值和最高平均值的摘要。Java阅读文件/扫描器
输入如下:
艾丽西亚商标,89,90,100,95,75,85,94,100,90,92
鲍比·里克斯,98,79,87,79,9 ,98,7,19,98,78
这里是放出来应该是什么样子(数字是不正确的,例如只):
艾丽西亚商标85.5乙
鲍比·里克斯90.0 A-
...
高98.2
低56.5
平均78.3333
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Test x = new Test();
x.high();
}
public void high()
{
File file2 = new File("scores.csv");
try
{
Scanner scanner = new Scanner(file2);
while (scanner.hasNextLine())
{
String line2 = scanner.nextLine();
String[] value2 = line2.split(",");
// Converts String Array into Double Array
double[] score = new double[value2.length];
for (int i = 1; i < value2.length; i++)
{
score[i] = Double.parseDouble(value2[i]);
}
// Finds the sum and then the average, adds it to a array List
ArrayList<Double> av;
av = new ArrayList<Double>();
double sumNum = 0.0;
for (double i : score)
{
sumNum += i;
}
double aver = sumNum/10;
av.add(aver);
double max = 0, min = 100;
for (int a = 0; a < av.size(); a++)
{
double s = av.get(a);
max = Math.max(max, s);
min = Math.min(min, s);
aver += s;
}
System.out.println("High " + max + "");
System.out.println("Low " + min + "");
System.out.println("Average " + aver/av.size() + "");
}
scanner.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
我觉得我有问题是与while循环,但我不是太确定了。任何帮助解决这个问题将非常感激。
你告诉我们你的需求,你告诉我们的代码,但你不要告诉我们你的代码有什么问题,这使我们很难帮助你。请告诉我们您的代码出了什么问题,它的行为不正确。 – 2013-05-05 19:47:21
为什么你'av = new ArrayList();'在每次迭代?把它放在'while'循环之外。 –
Maroun
2013-05-05 19:51:43
'这就是外观应该看起来像什么,但它看起来像什么? – 2013-05-05 19:52:51