我的一个类方法的结果总是出现0
。我知道这种情况可能是由于整数除法而发生的,并且我尝试将任何需要的数字转换为double
;但它仍然是0
。问题出在底部的 variance
方法上。这对我来说很奇怪,因为平均方法似乎可以很好地划分。
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;
}
}
最佳答案
我复习你的课。问题可能出在调用方法的顺序上:在调用 sum() 之前调用 Average() 将返回零(因为从未计算 sum)。为了避免这种情况,最好避免将结果存储在实例字段中,而是在必要时调用原始方法。前任。平均值 = sum()/长度。
要缓存结果(以提高性能),比这复杂一点。
这是您的类(class),有一些更改:
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;
}
}
免责声明:我没有运行它,所以先测试一下:-)
关于java - 使用类方法进行除法始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40703274/