变量已定义?

时间:2012-12-17 14:15:07

标签: java netbeans

在学习tpass参数时,我遇到了一个问题,我无法弄清楚为什么程序显示错误。我将重点介绍下面的代码。

public class pkgExone 
{
    static void displayMarkRange(int[] ref,double[] mark, double size,double topValue,
    double bottomValue, String message)
        {
            int index; //declare a variable index
              System.out.println("\n\n"+ message+"\n\n"); //print parameter message
                System.out.println("Reference \t Mark ");
                  System.out.println("Number \t Obtained\n");
                   // loop through the whole array and decide whether to print a students mark
        for(index=0;index<=size-1;index=index+1)
            {
              if ((mark[index]>= bottomValue) && (mark[index]<=topValue))
                {
                 System.out.println( ref[index]+" \t\t\t"+ mark[index]);
                } // end if construct
            } // end for loop
            System.out.println("\n\n");
        } // end of displayMarkRangeMethod

    static double Calaverage(double [] mark, double size)
    {
        int index;
        double total=0;
        double calculatedaverage;        
        int validentries=0;

        for (index=0;index<=size-1;index ++)

          if ((mark[index]>=1) && (mark [index] <=100))
           {
           **double total + mark [index];
           int validentries= validentries +1;**
           }
        **double calculatedaverage = total/validentires;**
        return calculatedaverage;


    }



    public static void main(String[] args) 
    {
        double [] ref; 
        double Averagemark;
        double howMany=10;
        int[] studentID = {900,901,902,903,904,905,906,907,908,909};
        double[] examMark = {23,45,56,90,83,114,48,39,26,102};

        displayMarkRange(studentID,examMark,howMany,100,1,"All Students that have valid marks");
        displayMarkRange(studentID,examMark,howMany,100,70,"All Distinction students");
        displayMarkRange(studentID,examMark,howMany,69,55,"All Merit students");
        displayMarkRange(studentID,examMark,howMany,54,40,"All Pass students");
        displayMarkRange(studentID,examMark,howMany,39,1,"All Fail students");
        displayMarkRange(studentID,examMark,howMany,69,40,"All Pass or Merit students");
        displayMarkRange(studentID,examMark,howMany,100,90,"Student that got between 90 and 100");
        displayMarkRange(studentID,examMark,howMany,10,1,"Student that got between 10 and 1");
        displayMarkRange(studentID,examMark,howMany,80,20,"Student that got between 80 and 20");
        displayMarkRange(studentID,examMark,howMany,100,**Averagemark**,"Students that got more than the average");
        displayMarkRange(studentID,examMark,howMany,Averagemark,1,"Students that got less than the average");
        displayMarkRange(studentID,examMark,howMany,120,101,"Students whos marks are invalid");
    }// end of psvm 
}//end of public class

当我编辑文件时:

Updating property file: H:\PIJOOP\ProjectSectionEleven\build\built-jar.properties
Compiling 1 source file to H:\PIJOOP\ProjectSectionEleven\build\classes
H:\PIJOOP\ProjectSectionEleven\src\projectsectioneleven\pkgExone.java:42: error: ';' expected
           double total + mark [index];
H:\PIJOOP\ProjectSectionEleven\src\projectsectioneleven\pkgExone.java:42: error: not a statement
           double total + mark [index];
2 errors
H:\PIJOOP\ProjectSectionEleven\nbproject\build-impl.xml:628: The following error occurred while executing this line:
H:\PIJOOP\ProjectSectionEleven\nbproject\build-impl.xml:246: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

4 个答案:

答案 0 :(得分:3)

这不是有效代码

double total + mark [index];

也许你打算

total += mark [index];

您只能为给定范围声明一次变量。

而不是

    **double calculatedaverage = total/validentires;**
    return calculatedaverage;

我会

return total / validentires;

为了您的兴趣,这就是我编写方法的方法。注意:size必须是int而不是双倍。

static double average(double... marks) {
    double total = 0;
    int count= 0;

    for(double mark: marks) {
        if (mark < 1 || mark > 100) continue;

        total += mark;
        count++;
    }
    return total / count;
}

您可以为所有标记调用

double average = average(marks);

或某些标记

double average = average(Arrays.copy(marks, size)); // note size must be an int.

答案 1 :(得分:0)

double total + mark [index]是句法无意义的。你可能意味着total += mark[index],因为你已经宣布了double total

类似的论点适用于您的第二个错误:您正在重新声明calculatedaverage。在这里,您只想删除double

我建议删除整个变量calculatedaverage并简单地写return total/validentires;

答案 2 :(得分:0)

正如它所说的那样看看@ this line **double total + mark [index];。不应该是total += mark[Index];吗?

仔细查看异常。

答案 3 :(得分:0)

以下语法无效:

       double total + mark [index];

我猜你想说:

       total += mark [index];

下一行也有问题:

       int validentries= validentries +1;

虽然语法上有效,但它会隐藏另一个validentries而不是更新它。使用:

       validentries= validentries +1;

最后,您calculatedaverage宣布了两次。您可以删除功能顶部显示的声明:

    double calculatedaverage;