0

メソッドを繰り返さずに、あるメソッドから別のメソッドに配列値を取得するのに少し苦労しています(メソッドはgetScannerInputを使用して審査員のスコアを取得します(他のオプションの代わりに私のユニで使用する必要があります:-/)。

私のプログラムは次のとおりです。

public class mark4

{

static int SIZE = 10; // Define array size
static int classMarks[] = new int [SIZE]; // Initialise array classMarks[]
static double max = 0; 
static double min = 0;
static double total = 0;
static double averagescore = 0;
static int pass = 0;

 public static int[] getMarks()
{
    int[] classMarks = new int [SIZE];

    for(int i = 0; i< classMarks.length; i++) // Start input loop to obtain marks
    {
        classMarks[i] = getScannerInput.anInt("Please enter students mark: ");
    }


        System.out.print("\nThe marks for this class are; "); // Output initial line to be completed with students marks

    for(int i=0; i<classMarks.length; i++) // Start output loop to output students marks
    {
        System.out.print(classMarks[i] + ", "); // Output marks separated by a comma and a space

    }

    return classMarks;
}

public static double averagescore(){

    for(int i=0; i<classMarks.length; i++) // Start loop to calculate total of all marks.
    {
        total = classMarks[i] + total; // Calculate total of array by traversing and adding to 'total' variable each time
        averagescore = total / classMarks.length; // Divide array total by array length to find average

    } // end average loop

        return averagescore;    

}

public static double min(){

    min = classMarks[0]; // Set min to first value of array to compare to rest


    for(int i=0; i<classMarks.length; i++) // Start loop to calculate minimum and maximum scores
    {
            if(classMarks[i] < min) // Compare values on each iteration, if value is less than current 'min' value, replace min with new value
                    min = classMarks[i];

    } // end minloop

    return min;
}

public static double max(){

    max = classMarks[0]; // Set max to first value of array to compare to rest


    for(int i=0; i<classMarks.length; i++) // Start loop to calculate minimum and maximum scores
    {
        if(classMarks[i]>max) // Compare values on each iteration, if value is greater than current 'max' value, repalce max with new value
                    max = classMarks[i];

    } // end max loop

    return max;
}

public static int pass(){

    for(int i=0; i<classMarks.length; i++) // Start loop to calculate how many students passed with a mark of 8 or higher
    {
            if( classMarks[i] >= 8 ) // Compare the current score in each iteration to 8 to see if it is more than or equal to the pass mark

                    pass++; // If current value is greater than or equal to pass mark, add one to pass counter.

    } // end pass loop

    return pass;
}

public static void main (String args[])
{

    int[] classMarks = getMarks();
    double averagescore = averagescore();
    double min = min();
    double max = max();
    int pass = pass();

                System.out.print("\nThe number of students who passed the exam is " + pass);
                System.out.printf("\nThe class average correct to 1 decimal place is: %4.1f", averagescore); // Output average score to 1 decimal place
                System.out.printf("\nThe lowest mark obtained in this class was: %3.1f", min); // Output lowest score to one decimal place (in case of half marks...)
                System.out.printf("\nThe highest mark obtained in this class was: %3.1f", max); // Output highest score to one decimal place (in case of half marks...)


} // end main

} // end class

プログラムは正しくコンパイルされますが、得られる結果はすべて0または0.0であり、getScores()メソッド中に入力された数値をメソッドが取得していないことを示しています。

を使用してメソッド内でclassMarks[]の結果を定義しようとすると。

int[] classMarks = getMarks();

getScores()メソッド全体を繰り返し、メインのメソッドに遭遇するたびに結果を再度要求します。

これはおそらくかなり単純な答えだと思いますが、評価の質問としてこれを行わなければなりませんでしたが、メソッドと配列に関する講義を見逃し、いくつかのマイナーな要素に少し苦労しています。

どんな助けでも大歓迎です!

4

2 に答える 2

3

あなたはこれを宣言します、それはあなたが使用しようとしているものであると私は信じています、

static int classMarks[] = new int [SIZE];

ただし、getMarks()内で、新しいものを宣言します。

int[] classMarks = new int [SIZE];

これは、そのメソッドの範囲内で使用されるものです。

クラススコープ変数を使用し、それをオーバーライドする他の変数を宣言しない(つまり、上記の2番目の宣言を削除する)か、min、maxなどのメソッドがint []引数を取る必要があり、 getMarks()から返された配列をそれらに渡すことができます。

于 2012-12-13T22:40:11.630 に答える
1

メインメソッドでは、classMarksを宣言しています。これは、クラスレベルで持っている静的なclassMarksを非表示にします。また、getMarksメソッド内で別のclassMarks配列を宣言しているため、スキャナーは共有classMarks配列を読み取ることはなく、getMarksのローカル配列のみを読み取ります。この配列は、メインのローカルclassMarks変数に返され、配置されますが、クラス内の他のすべてのメソッドによって参照される静的変数。

于 2012-12-13T22:38:42.693 に答える