Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 阵列平均数_Java - Fatal编程技术网

Java 阵列平均数

Java 阵列平均数,java,Java,我的代码应该计算数组值的平均值并将其打印出来。 我有 public class ArrayAverageTester { public static void main(String[] args) { int[] numArray = {12, 17, 65, 7, 30, 88}; // Create an ArrayAverage object and print the result ArrayAverage[] arr = {12, 1

我的代码应该计算数组值的平均值并将其打印出来。 我有

public class ArrayAverageTester
{

   public static void main(String[] args)
   {
      int[] numArray =  {12, 17, 65, 7, 30, 88};

     // Create an ArrayAverage object and print the result
     ArrayAverage[] arr = {12, 17, 65, 7, 30, 88};

     System.out.println("The average of the array is " + arr.getAverage()); 

   }
}
还有我的ArrayAverage班

public class ArrayAverage
{
   private int[] values;

   public ArrayAverage(int[] theValues)
   {
      values = theValues;
   }

   public double getAverage()
   {
     int sum = 0;  

     for(int value : values)
     {
       sum += value;
     }

   return (sum/values.length);
   }
}

我的错误是int无法转换为ArrayAverage,我忘记声明方法getAverage(),或者它超出了范围。

首先,您的
getAverage()
方法执行整数数学(当前)。你可以这样解决

public double getAverage() {
    int sum = 0;

    for (int value : values) {
        sum += value;
    }

    return (sum / (double) values.length);
}
接下来,您需要构造一个新的
ArrayAverage
实例(不是它们的数组),并将构造函数传递给数组进行平均。像

ArrayAverage arr = new ArrayAverage(numArray);
输出

The average of the array is 36.5

您的语法不正确
ArrayAverage[]
表示“数组的
ArrayAverage
s”。你应该:

  • 只需声明一个正常的
    int[]
  • 创建
    ArrayAverage
得到的错误是编译器正在查找在数组中定义的函数
getAverage()
,而不是
arrayaaverage

以下两种方法都应该有效:

public static void main(String[] args) {
    int[] values = {1, 2, 3, 4, 5};

    double count = 0;
    for (int i = 0; i < values.length; i++) {
        count += values[i];
    }

    count /= values.length;

    System.out.println(count);
}

公共类数组平均值{
私有int[]值;
公共阵列平均值(int[]值){
这个值=值;
}
公共双平均值(){
重复计数=0;
对于(int i=0;i
这是面向对象编程的一个基本问题,要测试ArrayAverage类,必须实例化一个对象(为此,它使用保留字“new”),错误在于您正在创建ArrayAverage数组

要使其按您的意愿工作,只需将numaray作为参数发送到ArrayAverage类

public static void main(String[] args) {
    int[] numArray =  {12, 17, 65, 7, 30, 88};

    // Create an ArrayAverage object and print the result
    ArrayAverage arr = new ArrayAverage(numArray);

    System.out.println("The average of the array is " + arr.getAverage()); 
}
答复

阵列的平均值为36.0

public class ArrayAverage {
    private int[] values;

    public ArrayAverage(int[] values) {
        this.values = values;
    }

    public double getAverage() {

        double count = 0;
        for (int i = 0; i < values.length; i++) {
            count += values[i];
        }
    return /= values.length;
  }
}
public static void main(String[] args) {
    int[] numArray =  {12, 17, 65, 7, 30, 88};

    // Create an ArrayAverage object and print the result
    ArrayAverage arr = new ArrayAverage(numArray);

    System.out.println("The average of the array is " + arr.getAverage()); 
}