0

在学习如何使用 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

4 回答 4

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.
于 2012-12-17T14:19:38.670 回答
0

double total + mark [index]是句法废话。您的意思可能是total += mark[index]因为您已经声明了double total.

类似的论点适用于您的第二个错误:您正在重新声明calculatedaverage. 在这里你只想放下double

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

于 2012-12-17T14:18:13.417 回答
0

正如它所说,看看@这一行**double total + mark [index];。不应该total += mark[Index];吗?

仔细查看异常。

于 2012-12-17T14:18:14.673 回答
0

以下在语法上无效:

       double total + mark [index];

我猜你的意思是:

       total += mark [index];

下一行也有问题:

       int validentries= validentries +1;

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

       validentries= validentries +1;

最后,你已经calculatedaverage宣布了两次。您可以删除出现在函数顶部的声明:

    double calculatedaverage;        
于 2012-12-17T14:19:01.397 回答