声明/初始化变量时出现.class错误?

时间:2013-09-15 15:30:24

标签: java

我正在学习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);
}
}

2 个答案:

答案 0 :(得分:4)

int momsAge= 42; dadsAge= 43;

应该是

int momsAge= 42, dadsAge= 43;

;作为声明的结尾,因此基本上是你的下一个声明

dadsAge = 43;

显然这是错误的,因为预计会上课。使用,可以链接这些作业。

此外:

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

未注释(至少http:部分未注释)。

答案 1 :(得分: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);
    }
}