简体   繁体   English

努力将数组值传递给方法

[英]Struggling with passing array values into methods

i'm struggling a bit with getting array values from one method into another without repeating the method over again (the method obtains judges scores using getScannerInput (we have to use it at my uni instead of other options :-/ ). 我正在努力将数组值从一种方法获取到另一种方法而又没有重复一遍(该方法使用getScannerInput获取法官得分(我们必须在我的uni而不是其他选项中使用它:-/)而在努力。

My program is the following; 我的程序如下;

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

The program compiles correctly, but the results I get are all 0 or 0.0, suggesting that the methods aren't taking the figures input during the getScores() method. 该程序可以正确编译,但我得到的结果全为0或0.0,这表明该方法未使用getScores()方法期间的数字输入。

If I try to define the results for classMarks[] inside the methods using; 如果我尝试使用以下方法在classMarks []中定义结果:

int[] classMarks = getMarks();

it repeats the whole getScores() method and asks for the results again every time it encounters a method in the main. 它重复整个getScores()方法,并在每次遇到main方法时都再次询问结果。

I'm aware this is probably a fairly simple answer, but we had to do this as a question in an assessment, but i missed the lectures on methods and arrays and i'm struggling a bit with some minor elements. 我知道这可能是一个相当简单的答案,但是我们必须在评估中将其作为一个问题来做,但是我错过了关于方法和数组的讲座,并且我在一些次要元素上苦苦挣扎。

Any help would be much appreciated! 任何帮助将非常感激!

You declare this, which I beleive is what you intend to use, 您声明这一点,我相信这是您打算使用的内容,

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

but, within getMarks(), you declare a new one: 但是,在getMarks()中,您声明了一个新的:

int[] classMarks = new int [SIZE];

which is what will be used within the scope of that method. 这将在该方法的范围内使用。

Either, you should use the class scope variable, and not declare others which will override it (that is, remove the second declaration shown above), or your min, max, etc. methods should take the an int[] argument, and you could pass the array returned from getMarks() to them. 或者,您应该使用类范围变量,而不要声明其他将覆盖它的变量(即,删除上面显示的第二个声明),或者您的min,max等。方法应采用int []参数,并且您可以将getMarks()返回的数组传递给他们。

In your main method you are declaring classMarks, which is hiding the static classMarks you have at the class level. 在您的主要方法中,您要声明classMarks,这将隐藏您在类级别具有的静态classMarks。 You are also declaring another classMarks array within your getMarks method, so your scanner is never reading into the shared classMarks array, only into the local array in getMarks, which is then returned and placed in the local classMarks variable in main, but never getting into your static variable that is referenced by every other method in the class. 您还要在getMarks方法中声明另一个 classMarks数组,因此您的扫描器永远不会读入共享的classMarks数组,而只会读入getMarks中的本地数组,然后将其返回并放置在main中的本地classMarks变量中,但永远不会进入您的静态变量,该静态变量由类中的所有其他方法引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM