我正在努力将数组值从一种方法转换为另一种方法而不重复该方法(该方法使用getScannerInput获得判断分数(我们必须在我的uni中使用它而不是其他选项: - /)。
我的计划如下;
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中的方法时再次询问结果。
我知道这可能是一个相当简单的答案,但我们不得不在评估中将其作为一个问题,但我错过了关于方法和数组的讲座,而且我对一些小元素进行了一些努力。< / p>
非常感谢任何帮助!
答案 0 :(得分:3)
你宣布这个,我相信你打算使用它,
static int classMarks[] = new int [SIZE];
但是,在getMarks()中,您声明了一个新的:
int[] classMarks = new int [SIZE];
将在该方法的范围内使用。
要么你应该使用类范围变量,而不是声明覆盖它的其他人(即删除上面显示的第二个声明),或者你的min,max等方法应该采用一个int []参数,你可以将从getMarks()返回的数组传递给它们。
答案 1 :(得分:1)
在你的main方法中,你声明了classMarks,它隐藏了你在类级别拥有的静态classMarks。您还在getMarks方法中声明另一个 classMarks数组,因此您的扫描程序永远不会读入共享的classMarks数组,只会读入getMarks中的本地数组,然后返回并放入本地classMarks变量中在main中,但永远不会进入由类中的每个其他方法引用的静态变量。