0

我正在学习java。今天,我的任务是找出一些代码中的错误。我正在处理它,但我不知道为什么会出现以下错误。在示例代码中,它在“int momsAge= 42; dadsAge= 43;”行上给了我一个“.class expected” 具体来说,它在 momsAge 前面放置了一条错误行。

// A class that computes the sample statistics of the ages of
// family members.
public class FamilyStats
{
public static void main(String[] args)
{
/*
math equations obtained from: */
http://mathworld.wolfram.com/SampleVariance.html

// define some ages

int momsAge= 42; dadsAge= 43;
int myAge= 22, sistersAge= 16;
int dogsAge= 6;

// get the mean

double ageSum = (momsAge + dadsAge + myAge + sistersAge + DogsAge);
double average = ageSum / 5

/ calculate the sample variance
double variance= 0.0;
variance += (momsAge - average)*(momsAge - average);
variance += (dadsAge - average)(dadsAge - average);
variance += (myAge - average)*(myAge - average);
variance += (sistersAge - average)*(sistersAge - average);
variance += (dogsAge - average)*(dogsAge - average);
variance = variance / 4;

// get the std. dev
double standardDev= Math.sqrt(variance);
// output the results
System.out.println("The sample age mean is: " + average);
System.out.println("The sample age variance is: " + variance);
System.out.println("The sample age standard deviation is: " + standardDev);
}
}
4

2 回答 2

4
int momsAge= 42; dadsAge= 43;

应该

int momsAge= 42, dadsAge= 43;

;用作语句的结尾,因此基本上是您的下一个语句

dadsAge = 43;

显然这是错误的,因为需要一个类。使用,允许您链接这些分配。

还:

http://mathworld.wolfram.com/SampleVariance.html

没有评论(至少http:部分没有)。

于 2013-09-15T15:31:30.637 回答
0

这是我所做的内联更正的正确代码:

// A class that computes the sample statistics of the ages of
// family members.
public class FamilyStats {

    public static void main(String[] args) {
        /*
         math equations obtained from: 
         http://mathworld.wolfram.com/SampleVariance.html
         */ 
       //^--Included the url into the comments
// define some ages
        int momsAge = 42, dadsAge = 43;
                      //^--Changed ; to ,
        int myAge = 22, sistersAge = 16;
        int dogsAge = 6;

// get the mean

        double ageSum = (momsAge + dadsAge + myAge + sistersAge + dogsAge);
        double average = ageSum / 5;
        // calculate the sample variance
       //^--Added the second / to mark the line as a comment
        double variance = 0.0;
        variance += (momsAge - average) * (momsAge - average);
        variance += (dadsAge - average) * (dadsAge - average);
        variance += (myAge - average) * (myAge - average);
                                    //^--Added missing * symbol
        variance += (sistersAge - average) * (sistersAge - average);
        variance += (dogsAge - average) * (dogsAge - average);
        variance = variance / 4;

// get the std. dev
        double standardDev = Math.sqrt(variance);
// output the results
        System.out.println("The sample age mean is: " + average);
        System.out.println("The sample age variance is: " + variance);
        System.out.println("The sample age standard deviation is: " + standardDev);
    }
}
于 2013-09-15T16:02:54.557 回答