简体   繁体   English

返回低于平均值的数量

[英]Return the amount of numbers below average

I am trying to write a program that returns the amount of numbers less than the average 我正在尝试编写一个程序,该程序返回的数量少于平均值

For example, if I have the numbers 2, 3 and 4, the average would be (2.1+3.6+4.2)/3 = 3.3 and since 2.3 is below average it would return 1 as there is one number below the average. 例如,如果我有数字2、3和4,则平均值将为(2.1 + 3.6 + 4.2)/ 3 = 3.3,并且由于2.3低于平均值,因此将返回1,因为存在一个低于平均值的数字。

I am getting an error that says 我收到一条错误消息:

Type mismatch: cannot convert from double[] to int

My code: 我的代码:

public static void main(String[] args) {

    double[] numbers = {2.1, 3.6, 4.2};

    System.out.println(belowaverage(numbers));
}



public static int belowaverage(double[] ba) {
    double sum = 0;
    double average = 0;

    for(int i = 0;i<ba.length;i++){
        sum = sum + ba[i];
        average = sum / ba.length;
        if(ba[i]<average){
        return ba;
    }
}

You're trying to return the array ba which is the array holding your input data instead of the count. 您正在尝试返回数组ba ,它是保存输入数据而不是计数的数组。

You need to leave the computation of the average in your current for loop and then create a second for loop and an int count variable which you will increment each time you find a number in the ba array that is smaller than the average. 您需要将平均值的计算留在当前的for循环中,然后创建第二个for循环和一个int count变量,每次在ba数组中找到一个小于平均值的数字时,该变量都会递增。 Then outside of that loop you return count . 然后在该循​​环之外,您返回count

Also this line: 也是这一行:

average = sum / ba.length;

Has to be outside of the first loop. 必须在第一个循环之外。

@Edit: others provided some code but it had either logical or compile time errors (not all of them I guess, the ones I checked) so here's a working version: @Edit:其他人提供了一些代码,但是它存在逻辑或编译时错误(我想并不是所有的我都检查过),所以这是一个工作版本:

public static int belowaverage(double[] ba) {
    double sum = 0;
    double average = 0;
    int count = 0;

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

    for(int i = 0; i < ba.length; i++){
        if (ba[i] < average) {
            count++;
        }
    }
    return count;
}

You don't need to cast length to double as sum is of type double so the result will be promoted to the bigger type. 您无需将长度强制转换为double因为sum是double类型的,因此结果将被提升为更大的类型。

You need to work out the sum first, then compute the average and then count how many below this threshold. 您需要先计算总和,然后计算平均值,然后计算低于此阈值的数量。

try 尝试

public static int belowaverage(double[] ba) {
  double sum = 0;
  double average = 0;
  int count = 0;

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

  for(int i = 0;i<ba.length;i++){
   if (ba[i] < average) count++;
  }

  return count;

}
public static void main(String[] args) {
    double[] numbers = {2.1, 3.6, 4.2};
    System.out.println(belowaverage(numbers));
}

public static int belowaverage(double[] ba) {
    double sum = 0;

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

    double average = sum / length; 
    int belowAvgCount = 0;
    for (int i = 0; i < length; i++) {
        if (ba[i] < average) {
            belowAvgCount++;
        }
    }        
    return belowAvgCount;
}

This isn't going to work using only a single for loop, because you can't possibly compare anything to the average until you've calculated it. 仅使用单个for循环将无法正常工作,因为在计算出平均值之前,您不可能将任何内容与平均值进行比较。

Try separating your calculation of the average and the counting of terms below the average into two different loops: 尝试将平均值的计算和平均值以下的项的计数分为两个不同的循环:

 public static int belowaverage(double[] ba) {
    double sum = 0;
    double average = 0;
    for(double b : ba){
        sum += b;
    }
    average = sum / ba.length;
    int count = 0;
    for(double b : ba){
        if(b < average){
            count++;
        }
    }
    return count;
}

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

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