gpt4 book ai didi

java - 使用数组和可变长度形式参数。 (java :38: error: '.class' expected)

转载 作者:行者123 更新时间:2023-11-30 04:24:33 24 4
gpt4 key购买 nike

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

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

^ 始终指向金额。

最佳答案

我发现的一个问题是您调用 calcStandardDeviation 方法的方式 -

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

实际上应该是 -

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

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

关于java - 使用数组和可变长度形式参数。 (java :38: error: '.class' expected),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16256923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com