简体   繁体   English

如何找到列表java的平均值

[英]how to find average of a List java

I have a question on how to find the average of a List. 我对如何找到列表的平均值有疑问。 I don't know what is wrong with my code, because I did the same for all the three averages but only the first one works, the others just show 0 , as shown below in my output: 我不知道我的代码有什么问题,因为我对所有三个平均值进行了相同的操作,但是只有第一个平均值有效,其他平均值仅显示0 ,如下所示:

Average:                                  58                                  0                                  0

Below is my code: 下面是我的代码:

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);
}
}

How can I fix this? 我怎样才能解决这个问题? Any help will be greatly appreciated. 任何帮助将不胜感激。

I can't see your input, but.... 我看不到您的输入,但是....

I don't think your for loop is doing what you want it to. 我不认为您的for循环正在做您想要的事情。 Each iteration, it is completely overwriting what is in averageCategory, averagePressure, averageWindspeed with whatever is on the right side of the equals sign. 每次迭代,都会用等号右侧的内容完全覆盖averageCategory,averagePressure,averageWindspeed中的内容。

Is this is what you meant to do? 这就是你的意思吗? Usually in calculating averages we get a sum and then divide once by the total number of items. 通常,在计算平均值时,我们求和,然后除以项目总数一次。

You are doing wrong here: 您在这里做错了:

 double average = sum / pressure.size();

In this way you are doing integer division, which is not good when computing average, expecially if the values are near to 1. You need to cast it: 这样你在做整除,计算平均值时,expecially如果值接近1,则需要投它这是不好的:

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

also you need to cast here: 你也需要在这里投:

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));
     }

Also, this last piece of code doesn't seem to calculate any average. 而且,这最后一段代码似乎没有计算任何平均值。 You should calculate the sum and divide it for the size of data. 您应该计算总和并将其除以数据大小。 But remember to cast integer division to double as I shown. 但记得要投整数除法,因为我出一倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM