数组的总和和平均值

时间:2016-03-11 18:39:16

标签: java arrays sum average

我正在研究大学课程的阵列问题,我正试图找到数组的总和和平均值。这是我到目前为止的代码。

public class Module55
{
   public static void main(String args[])
   {

   //declare the array to have 10 items for 10 weeks
   //also declare sum as an int and average as a double to accommodate for decimal points

      int[] weeks = new int[10];
      int sum = 0;
      double average = 0;

   //declare the array values

      weeks[0]= 2;
      weeks[1]= 4;
      weeks[2]= 8;
      weeks[3]= 10;
      weeks[4]= 14;
      weeks[5]= 16;
      weeks[6]= 20;
      weeks[7]= 22;
      weeks[8]= 24;
      weeks[9]= 26;

   // determine sum of the array values

     for (int index = 0; index < weeks.length; index++) weeks[index] = index;
        sum = sum + weeks[index];
        System.out.println("The total miles ran in 10 weeks is " + sum);

   // determine the average of the array values

     if (weeks.length != 0)
        average = sum / weeks.length;
     else
        average = 0;

     System.out.println("The average of the miles ran in 10 weeks is " + average);
     }
}

我知道平均值有效,但我总是坚持下去。我遇到的问题是我似乎无法初始化数组中的值,以便我可以为它们提供一个总和。我做错了什么或我错过了什么?

8 个答案:

答案 0 :(得分:2)

你需要在for循环中使用括号。目前您的代码正在评估如下:

for (int index = 0; index < weeks.length; index++) 
{
    weeks[index] = index;
}
sum = sum + weeks[index];
System.out.println("The total miles ran in 10 weeks is " + sum);

您希望您的代码像这样进行评估

for (int index = 0; index < weeks.length; index++) 
{
    weeks[index] = index; //logical issue, what does this line achieve?
    sum = sum + weeks[index];
}
System.out.println("The total miles ran in 10 weeks is " + sum);

这至少可以解决您的程序问题,但您仍需要查看您的逻辑。尝试使用断点来调试代码。

答案 1 :(得分:2)

您的代码中存在两个问题;

A)For循环分配

您不需要进行第一次分配,只需在周中添加总和[index]即可;

    for (int index = 0; index < weeks.length; index++)
        sum = sum + weeks[index];

B)计算平均值

Sum被定义为int,它是一个原始整数,因此,将整数除以整数,输出是一个不精确的整数。除法(45/10)的输出被转换为整数,然后被赋值为double,四舍五入为4,然后再次转换为double,并且'4.0'成为结果。

为了避免这种不恰当的结果,请将总和转换成双倍,如下所示;

average = (double)sum / weeks.length;

您的代码的更正版本如下;

演示

public class Module55 {
    public static void main(String args[]) {

        // declare the array to have 10 items for 10 weeks
        // also declare sum as an int and average as a double to accommodate for
        // decimal points

        int[] weeks = new int[10];
        int sum = 0;
        double average = 0;

        // declare the array values

        weeks[0] = 2;
        weeks[1] = 4;
        weeks[2] = 8;
        weeks[3] = 10;
        weeks[4] = 14;
        weeks[5] = 16;
        weeks[6] = 20;
        weeks[7] = 22;
        weeks[8] = 24;
        weeks[9] = 26;

        // determine sum of the array values
        for (int index = 0; index < weeks.length; index++)
            sum = sum + weeks[index];

        System.out.println("The total miles ran in 10 weeks is " + sum);

        // determine the average of the array values
        if (weeks.length != 0)
            average = (double)sum / weeks.length;
        else
            average = 0;

        System.out.println("The average of the miles ran in 10 weeks is " + average);
    }
}

输出

The total miles ran in 10 weeks is 146
The average of the miles ran in 10 weeks is 14.6

并注明范围

关于范围的最后一点说明,请查看此代码;

 for (int index = 0; index < weeks.length; index++)
    weeks[index] = index;
    sum = sum + weeks[index];
    System.out.println("The total miles ran in 10 weeks is " + sum);

for-loop中,因为没有使用括号,编译器只会在循环的for-loop中考虑scope下的第一个语句。这就是为什么,对于下一行,编译器会给出关于索引的错误,因为索引是在scope的{​​{1}}内定义的。

答案 2 :(得分:2)

在Java 8中实现这一目标的一种非常简单的方法是使用内置机制来收集统计信息:

int[] weeks = {3, 4, 6, 9, 10};
IntSummaryStatistics stats = IntStream.of(weeks).summaryStatistics();
System.out.println("sum = " + stats.getSum() + "; average = " + stats.getAverage());

答案 3 :(得分:1)

for (int i = 0;i < weeks.length) {
    sum += weeks[i];
}

System.out.println("Sum is:" + sum);

答案 4 :(得分:1)

首先,你根本不需要行周[index] = index;。 而且对于平均而言,如果你想将平均值设为double,那么你必须将总和加成两倍,因为你已经将总和声明为int。

public class Module55
{
   public static void main(String args[])
   {

   //declare the array to have 10 items for 10 weeks
   //also declare sum as an int and average as a double to accommodate for decimal points

      int[] weeks = {2,4,8,10,14,16,20,22,24,26};
      int sum = 0;
      double average = 0;

   // determine sum of the array values

     for (int index = 0; index < weeks.length; index++) 
         //weeks[index] = index;
         sum = sum + weeks[index];
         System.out.println("The total miles ran in 10 weeks is " + sum);

   // determine the average of the array values

     if (weeks.length != 0)
        average = (double)sum / weeks.length;
     else
        average = 0;

     System.out.println("The average of the miles ran in 10 weeks is " + average);
     }
}

10周内的总里程为146

10周内的平均英里数为14.6英里/小时

答案 5 :(得分:1)

对数字数组求和并除以n以获得这样的平均值将得不到正确的值 - 您不应使用整数除法计算平均值。 此外,这种方法可能适用于所示的示例,但不是一般的。例如,尝试使用此代码查找这两个值的平均值:(INT_MAX-6)和(INT_MAX-2)。

答案 6 :(得分:1)

你可以用lambdas找到这个方便。代码看起来像这样。

int weeks[] = {1,2,3,4};
List<Integer> asd = IntStream.of(weeks).boxed().collect(Collectors.toList());
//asd.forEach(System.out::println);
//this outputs average
System.out.println(asd.stream().mapToDouble(val -> val).sum()/asd.size());
//this outputs sum
System.out.println(asd.stream().mapToInt(val -> val).sum());
//another way to achieve this thanks to commenter
System.out.println(IntStream.of(asd).summaryStatistics());

答案 7 :(得分:1)

更正的代码。在计算平均值时,你已经将变量转换为其他两倍,你将得到平均值为整数

public class Mod55 {

    public static void main(String args[]) {

        //declare the array to have 10 items for 10 weeks
        //also declare sum as an int and average as a double to accommodate for decimal points
        int[] weeks = new int[]{2, 4, 8, 10, 14, 16, 20, 22, 24, 26};
        int sum = 0;
        double average = 0;

        // determine sum of the array values
        for (int index = 0; index < weeks.length; index++) {
            sum += weeks[index];
        }
        System.out.println("The total miles ran in 10 weeks is " + sum);

        // determine the average of the array values
        if (weeks.length != 0) {
            average = (double)sum / weeks.length;
        } else {
            average = 0;
        }

        System.out.println("The average of the miles ran in 10 weeks is " + average);
    }
}

Java8你可以做到这样的事情。

        int[] weeks = new int[]{2, 4, 8, 10, 14, 16, 20, 22, 24, 26};
        int sum = Arrays.stream(weeks)
                 .sum();
        double average = Arrays.stream(weeks).average().orElse(0);
相关问题