使用类方法的除法总是为零

时间:2016-11-20 11:04:56

标签: java

我的一个类方法的结果总是出现0。我知道可能会因为整数除法而发生这种情况,并且我已尝试将double所需的任何数字投射出去;但它仍保持0。问题在于底部的variance方法。这对我来说很奇怪,因为Average方法看起来很好。

public class MyArray {

    private int[] array;
    private int count = 0;
    public int length;
    public double sum;
    public double average;
    public double sqSum;
    public double variance;

   //Constructor to instantiate a new array        
   public MyArray(int length) {
       array = new int[length];
   }

   public int length(){
       return array.length;
   }


   //getter for count
   public int getCount(){
       return count;
   }

   //setter for count
   public void setCount(int Count){
       this.count = Count;
   }

   //getter for integers
   public  int[] getInts(){
       return array;
   }

   //setter for integers
   public void setInts(int counter, int input) {
       array[counter] = input;
   }

   public int getArray(int position) {
    return array[position];
   }

   public void setArray(int counter, int element){
       array[counter] = element;
   }
   public int intAt(int index) {
       return array[index]; 

   }
   //Method to print Arrays
   public void printArray() {   
       System.out.println("\nThe length of the array is "        
      +    array.length);

        for(int i=0; i<array.length; i++)
            System.out.println("The array is:" + array[i]);
   }

//Method to check if array is sorted
   public void isSortedArray() {   
         for(int i=0; i<array.length; i++)
             System.out.println("The array is:" + array[i]);
   }

//method to calc sum
   public double sum() {
         for(int i=0; i<array.length;i++)
             sum+= array[i];

        return (double)sum;
   }


    //method to calc Average
    public double average() {
        average =sum/array.length;
            return (double)average;
    }

    //method to calc sqSum
    public double sqSum() {
        double sqSum = 0.0;
        for(int i=0; i<array.length; i++)

           sqSum = sqSum +array[i] * array[i];

       return (double)sqSum;
    }    

     //method to calculate variance
     public double variance() {
            variance = (double)sqSum/array.length;

           return (double)variance;
     }
 }

1 个答案:

答案 0 :(得分:1)

我复习你的课程。可能问题在于调用方法的顺序:在调用sum()之前调用average()将返回零(因为sum从未计算过)。为避免这种情况,最好避免将结果存储在实例字段中,而是在必要时调用原始方法。防爆。 average = sum()/ length。

要缓存结果(为了提高性能),它比这更复杂。

这是您的课程,但有一些变化:

public class MyArray {

    private int[] array;

    // Constructor to instantiate a new array
    public MyArray(int length) {
        array = new int[length];
    }

    public int length() {
        return array.length;
    }

    // getter for integers
    public int getInts(int counter) {
        return array[counter];
    }

    // setter for integers
    public void setInts(int counter, int input) {
        array[counter] = input;
    }

    // getter for entire array
    public int[] getArray() {
        return array;
    }

    // setter for entire array
    public void setArray(int[] newArray) {
        this.array = newArray;
    }

    public int intAt(int index) {
        return array[index];
    }

    // Method to print Arrays
    public void printArray() {
        System.out.println("\nThe length of the array is " + array.length);

        for (int i = 0; i < array.length; i++)
            System.out.println("array[" + i + "] = " + array[i]);
    }

    // Method to check if array is sorted
    public boolean isSortedArray() {
        boolean isSorted = true;
        for (int i = 1; i < array.length; i++)
            if (array[i] > array[i-1]) {
                isSorted = false;
                break;
            } 
        System.out.println("The array is " + (isSorted ? "sorted" : "not sorted"));
        return isSorted;
    }

    // method to calc sum
    public long sum() {
        long sum = 0L;
        for (int i = 0; i < array.length; i++)
            sum += array[i];
        return sum;
    }

    // method to calc Average
    public double average() {
        return (double) sum() / array.length;
    }

    // method to calc sqSum
    public long sqSum() {
        long sqSum = 0L;
        for (int i = 0; i < array.length; i++)
            sqSum += array[i] * array[i];
        return sqSum;
    }

    // method to calculate variance
    public double variance() {
        return (double) sqSum() / array.length;
    }
}

免责声明:我没有运行它,所以先测试一下: - )