1

我还没有完成这个程序的工作,所以我知道可能还有其他错误。我目前收到“.class”预期错误,由于某种原因,我看不到那个错误。
我正在使用 jGrasp,这是我的 Java 简介类的一个项目。

import java.util.*;
import static java.lang.Math.*;

public class Final_Project_Test_Process_With_Arrays
        //Final Project
        //Mean and Standard Deviation
        //13.04.27
{
    public static void main(String[] args) {
        //Allow for the user to input numbers
        Scanner kb = new Scanner(System.in);

        System.out.println("How many numbers?");
        int amount = kb.nextInt();
        double[] nums = new double[amount];

        for (int count = 0; count < nums.length; count++) {
            System.out.print("Enter number " + (count + 1) + ": ");
            nums[count] = kb.nextDouble();
        }

        System.out.print("The mean of ");
        for (int index = 0; index < nums.length; index++) {
            System.out.print(nums[index] + ", ");
        }
        System.out.printf("is " + "%.2f", calcMean(nums));
        System.out.println();


        System.out.print("The Standard Deviation of ");
        for (int counter = 0; counter < nums.length; counter++) {
            System.out.print(nums[counter] + ", ");
        }
        System.out.print("is " + calcStandardDeviation(amount, mean,double...nums));
        System.out.println();

    }

    public static double calcMean(double[] nums) {
        double sum = 0;
        double mean = 0;
        for (int index = 0; index < nums.length; index++) {
            sum = sum + nums[index];
        }

        if (nums.length != 0)
            mean = sum / nums.length;

        return mean;
    }

    public static double calcStandardDeviation(int amount, double mean, double... nums) {
        double squared = 0;
        double sum = 0;
        double radicand = 0;
        for (double num : nums) {
            squared = pow((num - mean), 2);
            sum = sum + squared;
        }
        radicand = sum / amount;

        double standardDeviation = sqrt(radicand);

        return standardDeviation;
    }
}

我知道有一个简单的答案,并且我已经进行了搜索以提供帮助,但是作为一个初学者,我需要很长时间才能将其他类似帖子的答案应用于我自己的问题。

错误信息如下:

Final_Project_Test_Process_With_Arrays.java:38: error: '.class' expected
    System.out.print("is " + calcStandardDeviation(amount, mean, double ... nums)); 
                                               ^                             
Final_Project_Test_Process_With_Arrays.java:38: error: ')' expected
    System.out.print("is " + calcStandardDeviation(amount, mean, double ... nums)); 
                                                  ^                             
Final_Project_Test_Process_With_Arrays.java:38: error: ';' expected
    System.out.print("is " + calcStandardDeviation(amount, mean, double ... nums)); 
                                                    ^                               
Final_Project_Test_Process_With_Arrays.java:38: error: illegal start of expression
    System.out.print("is " + calcStandardDeviation(amount, mean, double ... nums)); 
                                                     ^                               4 errors  

^ 总是指向数量。

4

1 回答 1

2

我可以看到的一个问题是您调用该calcStandardDeviation方法的方式-

System.out.print("is " + calcStandardDeviation(amount, mean, double ... nums));

那实际上应该是——

System.out.print("is " + calcStandardDeviation(amount, mean, nums));

此外,您还没有声明变量mean(您正在传递给方法)。

于 2013-04-27T21:19:53.723 回答