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() 方法,并在每次遇到 main 方法时再次询问结果。

我知道这可能是一个相当简单的答案,但我们不得不在评估中将其作为一个问题来做,但我错过了关于方法和数组的讲座,而且我在一些次要元素上有点挣扎。

任何帮助将非常感激!

4

2 回答 2

3

你宣布这个,我相信这是你打算使用的,

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

但是,在 getMarks() 中,您声明了一个新的:

int[] classMarks = new int [SIZE];

这将在该方法的范围内使用。

要么,您应该使用类范围变量,而不是声明将覆盖它的其他变量(即,删除上面显示的第二个声明),或者您的 min、max 等方法应该采用一个 int[] 参数,并且您可以将 getMarks() 返回的数组传递给他们。

于 2012-12-13T22:40:11.630 回答
1

在您的主要方法中,您声明了 classMarks,它隐藏了您在类级别拥有的静态 classMarks。您还在 getMarks 方法中声明了另一个classMarks 数组,因此您的扫描仪永远不会读入共享的 classMarks 数组,只会读入 getMarks 中的本地数组,然后将其返回并放置在 main 中的本地 classMarks 变量中,但永远不会进入类中所有其他方法引用的静态变量。

于 2012-12-13T22:38:42.693 回答