计算均值和偏差的几乎完整代码

我只是对如何存储用户输入的数字数组感到困惑。用户可以在数组中输入他们想要的数字,但它本身并不要求输入数字。


import java.util.Scanner;


public class Exercise_0_2 {


public static void main(String[] args) {


Scanner input = new Scanner(System.in);


System.out.print("How many numbers: ");

int n = input.nextInt();


System.out.println("Enter  " + n + " numbers : ");

double[] numbers = new double[n];


//Call and print methods

System.out.println("Mean: " + mean(numbers, n));

System.out.println("Standard Deviation: " + deviation(numbers, n));


}

// Calculate Mean

public static double mean(double[] numbers, int n) {


    double sum = 0;


    for (int i = 0; i < n; i++) {

        sum += numbers[i];

    }

    double mean = sum / n;

    return mean;

    }

    //Calculate deviation from the mean

    public static double deviation(double[] numbers, int n) {


            double mean = mean(numbers, n);

            double sqSum = 0;

            for (int i = 0; i < n; i++) {

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

            }


                double variance = sqSum / n - mean * mean;

                double sd = Math.sqrt(variance);

                return sd;


    }

}


慕斯709654
浏览 143回答 2
2回答

阿波罗的战车

让我们从顶部回顾一下错误。&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;arr[]&nbsp;=&nbsp;new&nbsp;int[(int)&nbsp;n];此时n尚未声明,因此您不能在此处使用它。只需在声明并从扫描仪arr读取后将声明移动到。n顺便考虑int一下n.&nbsp;double真的没有意义。&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Mean:&nbsp;"&nbsp;+&nbsp;mean);如果这应该是对方法的调用,则mean需要在方法名称之后加上方括号,并将所需的参数放在方括号中,如mean(arr).&nbsp;mean(arr)仍然无法工作,因为arrhas typeint[]而该方法需要 type&nbsp;double[]。我无法告诉您正确的解决方法是什么,因为我不确定您是要阅读方法外部还是内部的数字。外面对我来说似乎更自然。&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Enter"&nbsp;+&nbsp;n&nbsp;+&nbsp;"&nbsp;numbers:&nbsp;");您不能在n此处使用该变量,因为它是main方法中的局部变量。再说一次,如果您打算在其他地方读取数字并将填充的数组传递给方法,我不确定您是否需要它。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array[i]&nbsp;=&nbsp;input.nextDouble();这里有两个错误:您没有名为array1.&nbsp;你的意思可能是numbers,我不确定。而且您不能在input此处使用该变量,因为它是main方法中的局部变量(与上述问题相同n)。要么将其声明为静态字段,要么将其作为参数传递给方法。方法中的错误deviation类似,不再赘述。既然我知道这对你来说应该是一个练习,我不会为你解决它并剥夺你的学习机会。我希望你能更进一步。编辑您的问题的编辑有一些很好的改进。你似乎还没有决定是读还是读数main字mean?&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Mean:&nbsp;"&nbsp;+&nbsp;mean(numbers,&nbsp;n));您的方法中没有numbers声明变量main(下一行中的问题相同)。否则很好。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;+=&nbsp;a[i];你可能的意思是meanSum和numbers?或者,也许您打算声明sum而不是在循环meanSum之上?for在上面的行之后缺少一个右花括号来结束第一个for循环。&nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;dqDiff&nbsp;=&nbsp;0;简单的错字:您稍后会引用此变量sqDiff,因此需要在声明中使用相同的拼写。&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Math.sqrt(mean(number,&nbsp;n));失踪。s_&nbsp;numbers我不相信这个简单的数学版本是正确的。请不要指望我在此代码上进行更多轮次。

慕森王

我可以建议首先使用 anArrayList而不是 an array[],在这种情况下,您无需指示数字 (n) 的数量,并且可以随意输入任意数量的数字。其次,使用 try-catch 块来停止输入数字和初始化计算。例如:public class MeanAndDeviation {public static void main(String[] args) {&nbsp; &nbsp; Scanner scn = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter the numbers then press any letter to calculate Mean and Sd");&nbsp; &nbsp; /*Use an ArrayList to freely enter so much numbers as you need*/&nbsp; &nbsp; ArrayList <Double> arr = new ArrayList <>();&nbsp; &nbsp; /*Use try-catch block to start calculations by entering any letter*/&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; while (scn.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double i = scn.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr.add(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /*Use catch (Exception e) to catch any type of exceptions*/&nbsp; &nbsp; catch (Exception e) {}&nbsp; &nbsp; System.out.println(arr + "\nMean: " + mean(arr) +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "\nStandard Deviation: " + deviation(arr));&nbsp; &nbsp; }private static double mean(ArrayList<Double> arr) {&nbsp; &nbsp; double sum = 0;&nbsp; &nbsp; for (int i = 0; i < arr.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; sum += arr.get(i);}&nbsp; &nbsp; double mean = sum/arr.size();&nbsp; &nbsp; return mean;&nbsp; &nbsp; &nbsp; &nbsp;}private static double deviation (ArrayList<Double> arr) {&nbsp; &nbsp; double sumOfDiff = 0;&nbsp; &nbsp; for (int i = 0; i < arr.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; sumOfDiff += Math.pow(arr.get(i) - mean(arr), 2);&nbsp; &nbsp; }&nbsp; &nbsp; double deviation = Math.sqrt(sumOfDiff/arr.size());&nbsp; &nbsp; return deviation;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java