0
import java.util.Scanner; 
import java.util.Arrays; 
public class Improved { 

    //I resize the array here so that it only counts inputs from the user
    //I want to ignore the 0 input from the user 
            //I think the error happens here or in my main method
    public static double[] resizeArray(double[] numbers, double size) {  
        double[] result = new double[(int)size];
        for (int i = 0; i < Math.min(numbers.length, size); ++i) {
            result[i] = numbers[i];
        }
        return result; 
    }

    //compute average nothing is wrong here
    public static double getAverage( double[] numbers) { 
        double sum = 0;
        for (int i = 0; i < numbers.length; ++i) 
            sum += numbers[i]; 
        double average = sum/numbers.length; 
        return average; 
    }

    //SD nothing is wrong here
    public static double getSD( double[] numbers, double average) { 
        double sd = 0; 
        for ( int i = 0; i < numbers.length; ++i) 
            sd += ((numbers[i] - average)*(numbers[i] - average)/ numbers.length); 
        double standDev = Math.sqrt(sd); 
        return standDev; 
    }

    //maximum nothing is wrong here
    public static double getMax( double[] numbers) { 
    double max = numbers[0]; 
    for (int i = 1; i < numbers.length; ++i)
        if (numbers[i] > max){
            max = numbers[i];
    }
    return max; 
    }

    //minimum nothing is wrong here
    public static double getMin( double[] numbers) { 
    double min = numbers[0]; 
    for (int i = 1; i < numbers.length; ++i)
        if (numbers[i] < min) {
            min = numbers[i];
        }
    return min; 
    }

    //median value nothing is wrong here
    public static double getmed( double[] numbers) { 
        double median;
        if (numbers.length % 2 == 0) 
            median = (((numbers[numbers.length/2 - 1]) 
                + (numbers[numbers.length/2]))/2);
        else
            median = numbers[numbers.length/2];

        return median; 
        }
//the problem is in the main method i think or in the call method to resize
public static void main(String[] args) { 

    Scanner input = new Scanner(System.in); 
    double[] statArr = new double[99]; 
    double size = 0;
    int i = 0;
    System.out.println("Type your numbers: ");
        double number = input.nextDouble(); 
        //I don't want the zero in the array, I want it to be excluded
        while (number != 0){
            statArr[i] = number;
            i++; 
            number = input.nextDouble();
            ++size;
        if ( size == statArr.length) { 
            statArr = resizeArray(statArr, statArr.length * 2); 
        }
        ++size;
    }
    statArr = resizeArray(statArr, size);

    java.util.Arrays.sort(statArr);

    double average = getAverage(statArr);

    System.out.println( "The average is " + getAverage(statArr));

    System.out.println( "The standard deviation is " +  getSD(statArr, average));

    System.out.println( "The maximum is "  + getMax(statArr));

    System.out.println( "The minimum is " + getMin(statArr));
    }
}

// 我对计算数学部分没有任何顾虑,但我似乎做不到,所以我的数组忽略了结束 while 循环的 0。换句话说,我希望每个数字都包含在内,直到用户输入数字 0。其他一切都是正确的。非常感谢!

4

1 回答 1

2

你有++size两次。这意味着您的resizeArray方法将无法正常工作:

double[] result = new double[(int)size];

在这里,您分配的比实际想要的要多。这就是为什么您在数组中得到零的原因。Java 数组初始化为 0(在数字原始类型的情况下)。

正如 Giodude 已经评论的那样,我建议您尽可能使用List实现(通常ArrayList)而不是数组。

size可以完全声明int并避免该演员表(并节省一些极少的内存),您不会将其用作double任何地方。

于 2013-11-06T21:42:14.200 回答