简体   繁体   English

如何计算数组中偶数的百分比?

[英]How to calculate the percentage of even numbers in an array?

i am beginner and here is the method I am struggling with. 我是初学者,这是我正在努力的方法。

Write a method called percentEven that accepts an array of integers as a parameter and returns the percentage of even numbers in the array as a real number. 编写一个名为percentEven的方法,该方法接受一个整数数组作为参数,并将数组中偶数的百分比作为实数返回。 For example, if the array stores the elements {6, 2, 9, 11, 3}, then your method should return 40.0. 例如,如果数组存储元素{6,2,9,11,3},那么您的方法应返回40.0。 If the array contains no even elements or no elements at all, return 0.0. 如果数组不包含偶数元素或根本没有元素,则返回0.0。

here is what I have so far... 这是我到目前为止所拥有的......

public static double percentEven(int[]a){
    int count = 0;
    double percent = 0.0;
    if (a.length > 0){
        for ( int i = 0; i < a.length; i++){
            if ( a[i] % 2 == 0){
                count++;
            }
        }
            percent  =(count/a.length)*100.0;
    }
            return percent;
}

i keep returning 0.0 when array contains a mix of even and odd elements but works fine for all even element array or all odd array? 当数组包含偶数和奇数元素的混合时,我继续返回0.0但是对于所有偶数元素数组或所有奇数数组都可以正常工作吗? i can't see where the problem is? 我看不出问题在哪里? thanks in advance. 提前致谢。

count/a.length returns 0 since you are dividing two ints, and the second operand is larger than the first. count/a.length返回0因为你要划分两个整数,第二个操作数大于第一个。 Change it to (double)count/a.length in order to perform floating point division. 将其更改为(double)count/a.length以执行浮点除法。

Alternately, change the order of operations to : 或者,将操作顺序更改为:

percent = 100.0*count/a.length;

@Bathsheba : Well said, thanks for the suggestion. @Bathsheba:好的,谢谢你的建议。

Here is sample code : 这是示例代码:

public class PercentEven {

    public static void main(String args[]){
        int count = 0;
        int[] a={2, 5, 9, 11, 0}; // this can be dynamic.I tried diff values 
        double percent = 0.0;
        if (a.length > 0){
            for ( int i = 0; i < a.length; i++){
                if ( a[i] % 2 == 0){
                    count++;
                }
            }
                percent  = (100*count/a.length);
        }
        System.out.println(percent);
    }
}

For a simple division like 2*100.0/5 = 40.0, the above logic would work fine but think about the situation where we have 51*100.0/83 the output would be less readable and its always advisable to truncate the percentage to a limited decimal digits. 对于像2 * 100.0 / 5 = 40.0这样的简单除法,上述逻辑可以正常工作,但考虑一下我们有51 * 100.0 / 83的情况,输出的可读性会降低,并且总是建议将百分比截断为有限小数数字。

An example: 一个例子:

int count = 51;
Double percent = 0.0;
int length = 83;
percent = count*100.0/length;

System.out.println(percent);

output: 61.44578313253012 输出:61.44578313253012

When you truncate it: 当你截断它时:

Double truncatedDouble = new BigDecimal(percent ).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(truncatedDouble);

output: 61.446 输出:61.446

List<Integer> numbers = Arrays.asList(a);
int number = numbers.stream().filter(n->n%2==0).count();
int percent = number*100.0/numbers.size();

I have done this in java 8 我在java 8中完成了这个

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

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