[英]How to calculate the average of all the non-negative numbers in the array
I have problem in calculate the average of all the non-negative numbers in the array, zero inclusive, and return zero otherwise.我在计算数组中所有非负数的平均值时遇到问题,包括零,否则返回零。 Below is my coding, please help me to check which parts is incorrect.
以下是我的编码,请帮我检查哪些部分不正确。 Thanks.
谢谢。
public class AverageOfNonNegativeNumbers {
public static double averageOfNumbers(double[] x) throws Exception {
double avg = 0.1;
if (x != null) {
for (double i = 0.1; i < x.length; i++) {
if ( x[i]%2 == 0 ) { //Anyone know how to set avoid calculate for negative numbers?
avg = avg / x[i]; //This code is calculate total average number.
}
}
}
return avg;
}
public static void main(String args[]) {
double x[] = {1.663, -2.1312, 3.13231, 4.124, -5.551, -6.1312, 7.111, 8.222, -9.01};
try {
System.out.println(AverageOfNonNegativeNumbers.averageOfNumbers(x));
} catch (Exception e) {
System.out.println("Error!!!");
}
}
}
Define 2 variables to store the sum
of non-negative numbers and the count
of non-negative numbers.定义 2 个变量来存储非负数的
sum
和非负数的count
。
Then iterate through the array and check each element for non-negativity.然后遍历数组并检查每个元素的非负性。 If non-negative, add that value to the
sum
and increment the count
by 1.如果非负数,则将该值添加到
sum
中并将count
加 1。
Finally, divide the sum
by count
to get the average.最后,将
sum
除以count
以获得平均值。
public static double averageOfNumbers(double[] x) {
double sum = 0; // Variable to store the sum
int count = 0; // Variable to keep the count
if (x != null) {
for (int i = 0; i < x.length; i++) {
double value = x[i];
if (value >= 0) { // Check if the number is non-negative
sum += value; // Add the value to the current sum
count++; // Increment the count by 1
}
}
}
return (count > 0) ? (sum /count) : 0; // Calculate the average if there were any non-negative numbers
}
Following are the issues:以下是问题:
a.you are trying to access the x[i], but i needs to be integer
type, while you have it as double
a.你正在尝试访问 x[i],但我需要是
integer
类型,而你有它作为double
b.湾。
x[i]%2 == 0
checks if number is even or not. x[i]%2 == 0
检查数字是否为偶数。 need to change that to x[i] >= 0
需要将其更改为
x[i] >= 0
c. c。 Logic to calculate average is not correct.
计算平均值的逻辑不正确。
public class AverageOfNonNegativeNumbers {
public static double averageOfNumbers(double[] x) {
int elements = 0;
double sum = 0;
if (x != null) {
for (int i = 1; i < x.length; i++) {
if ( x[i] >= 0 ) { // changed this check if number is negative or not
sum += x[i]; //This code calculates total sum of all non-negative numbers
elements++; // and also how many of such no exists
}
}
}
return sum/elements;
}
public static void main(String args[]) {
double x[] = {1.663, -2.1312, 3.13231, 4.124, -5.551, -6.1312, 7.111, 8.222, -9.01};
try {
System.out.println(AverageOfNonNegativeNumbers.averageOfNumbers(x));
} catch (Exception e) {
System.out.println("Error!!!");
}
}
}
You're trying to use a double as an index to an array;您正在尝试使用 double 作为数组的索引; this doesn't really make sense.
这真的没有意义。 An array must be indexed by an integer.
数组必须由 integer 索引。 That is, an array consists of an element in position 0, position 1, 2, etc.;
即一个数组由position 0、position 1、2等中的一个元素组成; it doesn't really make much sense to say 'the array element at position 0.1'.
说“position 0.1 处的数组元素”并没有多大意义。
To make your code compile, you will need to declare variable 'i' in your for loop in function 'averageOfNumbers' as an 'int' and not a 'double'.为了使您的代码编译,您需要在 function 'averageOfNumbers' 的 for 循环中将变量 'i' 声明为 'int' 而不是 'double'。
Modifications in you code: According to your question you want to calculate the average of all negative numbers including 0.代码中的修改:根据您的问题,您要计算所有负数的平均值,包括 0。
Errors:错误:
The way to calculate average is calculate the sum and count and then divide them to minimize the floating point errors.计算平均值的方法是计算总和和计数,然后将它们除以最小化浮点误差。
public static double averageOfNumbers(double[] x){ double avg = 0.0; double sum = 0.0; int count=0; if (x;= null){ for (int i = 0. i < x;length; i++){ if ( x[i]>=0 ){ sum = sum + x[i]; count=count+1; } } } if(count;=0){ avg=sum/count; } return avg; }
A simle one-Liner using Java 8 Strems:使用 Java 8 流的简单单线器:
double avg=Stream.of(x).mapToDouble(d->d).map(num->num<=0).average().orElse(0.0);
It creates a DoubleStream
( mapToDouble
) and removes all numbers greater than 0 and calculates the average.它创建一个
DoubleStream
( mapToDouble
) 并删除所有大于 0 的数字并计算平均值。
If there is no result(because x
is empty) it will return 0
.如果没有结果(因为
x
为空),它将返回0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.