如何查找List java的平均值

时间:2013-12-16 23:24:03

标签: java arraylist date-arithmetic

我有一个关于如何找到List平均值的问题。我不知道我的代码有什么问题,因为我对所有三个平均值都做了相同的操作,但只有第一个有效,其他只显示0,如下面的输出所示:

Average:                                  58                                  0                                  0

以下是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Hurricanes2{

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

    Scanner scan = new Scanner(new FileReader(new File("/Users/timothylee/hurcdata2.txt")));
    List<Integer> year = new ArrayList<Integer>();
    List<Integer> windspeed = new ArrayList<Integer>();
    List<Integer> pressure = new ArrayList<Integer>();
    List<String> name = new ArrayList<String>();
    List<Integer> category = new ArrayList<Integer>();

     while(scan.hasNext()){
         String input = scan.nextLine();
         String[] hurricane = input.split("\\s+");
         year.add(Integer.parseInt(hurricane[0]));
         windspeed.add(Integer.parseInt(hurricane[2]));
         pressure.add(Integer.parseInt(hurricane[3]));
         name.add(hurricane[4].trim());
         category.add(Integer.parseInt(hurricane[5]));
     }

     int sum = 0;
     for(Integer integer : pressure){
         sum += integer.intValue();
     }

     double average = sum / pressure.size();

     // create menu
     System.out.printf("%50s%n%n", "Hurricanes 1980 - 2006");
     System.out.printf("%1s%20s%20s%20s%20s%n", "Year", "Hurricane", 
             "Category", "Pressure(mb)", "Wind Speed (mph)");
     System.out.println("__________________________________________________"
             + "__________________________________");
     for(int i = 0; i < year.size(); i++){
         System.out.printf("%4d%20s%20d%20d%20d%n", year.get(i), name.get(i)
                 , category.get(i), pressure.get(i), windspeed.get(i));
     }
     System.out.println("__________________________________________________"
             + "__________________________________");
     int averageCategory = 0;
     int averagePressure = 0;
     int averageWindSpeed = 0;
     for(int i = 0; i < category.size(); i++){
         averageCategory = i / category.get(i);
         averagePressure = i / pressure.get(i);
         averageWindSpeed = i / windspeed.get(i);
     }
     System.out.printf("%1s%35d%35d%35d%n", "Average: ", averageCategory, 
             averagePressure, averageWindSpeed);
}
}

我该如何解决这个问题?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

我无法看到你的输入,但是......

我不认为您的for循环正在按照您的意愿行事。每次迭代,它都会完全覆盖averageCategory,averagePressure,averageWindspeed以及等号右侧的内容。

这是你的意思吗?通常在计算平均值时,我们得到一个总和,然后除以项目总数一次。

答案 1 :(得分:-1)

你在这里做错了:

 double average = sum / pressure.size();

通过这种方式,您可以进行整数除法,这在计算平均值时并不好,特别是如果值接近1。 你需要施展它:

 double average = (double)sum / (double)pressure.size();

你也需要在这里施放:

for(int i = 0; i < category.size(); i++){
         averageCategory = (int) ((double)i / (double)category.get(i));
         averagePressure =(int) ((double) i / (double)pressure.get(i));
         averageWindSpeed = (int) ((double)i / (double)windspeed.get(i));
     }

此外,最后一段代码似乎没有计算任何平均值。您应该计算总和并将其除以数据大小。但请记住,如我所示,将整数除法加倍。