使用数组奇数和偶数平均值 [英] even and odd averages using array

查看:352
本文介绍了使用数组奇数和偶数平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序,读取来自用户10的值的列表。把值在数组中。程序应该读阵列,然后计算并显示甚至输入值的平均值和奇输入值的平均值。这应该使用对象,方法来完成,而测试器类。

Write a program that reads a list of 10 values from the user. Put the values in an array. The program should read the array and then calculate and display the average of the even input values and the average of the odd input values. This should be done using objects, methods, and a tester class.

我想不通为什么我收到错误:

I cannot figure out why I am receiving the error:

不好操作类型二元运算。

bad operand types for binary operator.

我不知道是什么改变。我知道什么是错的我的MOD(%)。

I do not know what to change. I know something is wrong with my mod (%).

下面是我至今对我的平均类:

Here is what I have so far for my Average class:

public class Average 
{
    private int[] numbers = new int[10];
    double aveEven, aveOdd,sumEven=0,sumOdd=0; 
    int oddCounter=0, evenCounter=0; 
    public Average(int[] n)
    {
        numbers = n;

        if (numbers % 2 == 0)/something is wrong here/
        { 
            evenCounter++; 
            sumEven+=n; 

        } 
        else
        { 
            oddCounter++; 
            sumOdd+=n; 

        } 
    }

    public void aveEven()
    {
        for (int i = 0; i < numbers.length; i++)
        {
            aveEven = sumEven/evenCounter;
            System.out.println("The even average is: " + aveEven);
        }
    }

    public void aveOdd()
    {
        for(int i = l; i < numbers.length;  i++)
        {
            aveOdd = sumOdd/oddCounter;
            System.out.println("The odd average is: " + aveOdd);
        }
    }
}

对于 AverageTester 类我有以下几点:

import java.util.Scanner;
public class AverageTester 
{public static void main(String[] args) 
     {
        int[] integer = new int[10];

        Scanner input = new Scanner(System.in);

        for(int i=0 ; i < 10 ; i++)
        {
            System.out.print("Please enter a number : ");
            integer[i] = input.nextInt();
        }

        Average example = new Average(integer);
        example.aveOdd();


    }
}

此外,如果你看到任何东西,可能是错的,请让我知道。
谢谢你。

Also, If you see anything else that could be wrong, please let me know. Thank you.

推荐答案

数字是一个数组,所以号%2 是无效的。你应该阵列上环和使用%运算符的数组中的元素。在 + = 运营商也应该数组的元素上应用(即号码[I] ),而不是整个数组。

numbers is an array, so numbers % 2 is invalid. You should loop over the array and use the % operator on the elements of the array. The += operator should also be applied on a element of the array (i.e. numbers[i]) and not the entire array.

numbers = n;
for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] % 2 == 0) { 
        evenCounter++; 
        sumEven+=numbers[i]; 
    }  else { 
        oddCounter++; 
        sumOdd+=numbers[i]; 
    } 
}

至于 aveEven aveOdd ,既然你已经计算在构造函数中的款项(或至少它似乎像这就是你打算做),你不需要在这些方法中循环。

As for aveEven and aveOdd, since you already compute the sums in the constructor (or at least it seems like that's what you intended to do), you don't need a loop in these methods.

编辑:

我原本以为你旨在计算在阵列和数字的奇数位置上的平均在偶数位置上的数的平均值。重读这个问题后,我觉得奇/偶指的是数字本身,所以我相应地改变了code。

I originally assumed you intended to calculate the average of the numbers in even positions in the array and the average of the numbers in odd positions. After reading the question again, I think the odd/even refers to the numbers themselves, so I changed the code accordingly.

这篇关于使用数组奇数和偶数平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆