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];

特定のスコープに対して変数を宣言するのは1回だけです。

それ以外の

    **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];

次の行にも問題があります。

       int validentries= validentries +1;

構文的には有効ですが、validentries更新する代わりにもう一方を非表示にします。使用する:

       validentries= validentries +1;

最後に、calculatedaverage2回宣言しました。関数の上部に表示される宣言を削除できます。

    double calculatedaverage;        
于 2012-12-17T14:19:01.397 に答える
0

double total + mark [index]は構文ナンセンスです。total += mark[index]すでに宣言しているので、おそらく意味がありdouble totalました。

同様の議論が 2 番目のエラーにも当てはまります: you are redeclaring calculatedaverage. ここでは、ドロップしたいだけですdouble

ただし、変数全体を削除して、calculatedaverage単に次のように書くことをお勧めしますreturn total/validentires;

于 2012-12-17T14:18:13.417 に答える
0

それが言うように、@ this 行を見てください**double total + mark [index];。そうあるべきではありtotal += mark[Index];ませんか?

例外を注意深く見てください。

于 2012-12-17T14:18:14.673 に答える