我是大学一个java课程的助教,一个学生今天在实验室遇到了一个非常奇怪的问题。我查看了大约一个小时,让实验室中的其他TA做同样的事情,但我们找不到问题。
有效地我们在这里做的是创建3个数组,将它们传递给新方法。在新方法中修改那些数组的值并返回原始方法。我们没有使用return语句将任何数组返回到原始方法。相反,我们正在瞄准,我只能描述来自C背景作为传递参考。但是,在返回原始方法时,值已更改为某些不正确的值。
在这个具体的例子中,我们有三个数组:“考试”,“测验”和“实验室”。这些数组中的每一个都是大小为1,000并初始化为-1。在第一个方法“calcGrade”中,我们创建这些数组并初始化它们。然后我们将所有三个数组传递给第二个方法,该方法捕获用户拥有的考试,测验和实验室的数量,然后将实际考试,测验和实验室成绩值存储到数组中。
方法1(calcGrade)
exams quizzes labs
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
. . .
. . .
. . .
方法2(getScores)
exams quizzes labs
90 80 90
-1 80 90
-1 -1 -1
-1 -1 -1
. . .
. . .
. . .
返回方法1(calcGrades)
exams quizzes labs
80 90 90
-1 -1 90
-1 -1 -1
-1 -1 -1
. . .
. . .
. . .
任何人都可以想到这可能发生的任何原因吗?老实说,我很难过,我不希望他因为一些似乎没有错的东西而失去信誉......
以下是代码(请注意,其中有几个println语句用于调试目的):
import java.util.Scanner;
public class CSE1341Grade{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.println("What is your first name? ");
String first = input.nextLine();
System.out.println("What is your last name? ");
String last = input.nextLine();
calcGrade(first, last);
}
public static void calcGrade(String first, String last){
int base = 1000;
int[] quizzes = new int [base];
int[] exams = new int [base];
int[] labs = new int [base];
for(int x = 0; x < base; x++)
{
quizzes[x] = -1;
exams[x] = -1;
labs[x] = -1;
}
int[] countarr = getScores(quizzes, exams, labs);
System.out.println("EXAMS:");
for(int x = 0; x < countarr[0]; x++)
System.out.println(exams[x]);
System.out.println("QUIZ:");
for(int x = 0; x < countarr[1]; x++)
System.out.println(quizzes[x]);
System.out.println("LABS:");
for(int x = 0; x < countarr[2]; x++)
System.out.println(labs[x]);
for(int x = 0; x < countarr.length; x++)
System.out.println(countarr[x]);
//System.out.println("----");
double examAvg =0.0;
for(int i=0;i<countarr[0];i++){ //adding together scores
examAvg+=exams[i];
//System.out.println(examAvg);
}
//System.out.println("----");
double quizAvg=0.0;
for(int i=0;i<countarr[1];i++){ //adding together scores
quizAvg+=quizzes[i];
//System.out.println(quizAvg);
}
//System.out.println("----");
double labAvg=0.0;
for(int i=0;i<countarr[2];i++){ //adding together scores
labAvg+=labs[i];
//System.out.println(labAvg);
}
examAvg = examAvg/countarr[0];
quizAvg = quizAvg/countarr[1];
labAvg = labAvg/countarr[2];
double totalAverage = (.5 * examAvg) + (.35 * quizAvg) + (.1 *labAvg) + 5.0;
System.out.println("Total Score: " +totalAverage);//display average
String grade = "";
if (totalAverage >= 90)
grade = "A";
else if (totalAverage >= 80)
grade ="B";
else if (totalAverage >= 70)
grade = "C";
else
grade = "F";
System.out.println(first + " " + last + " your grade is a: " + grade); //letter grade
}
public static int [] getScores(int [] exams, int [] quizzes, int [] labs){
Scanner input = new Scanner(System.in);
int [] countArray = new int[3]; //holding numbers of exams quizzes labs
System.out.println("How many exam grades do you have? ");
countArray[0] = input.nextInt();
System.out.println("How many quiz grades do you have? ");
countArray[1] = input.nextInt();
System.out.println("How many lab grades do you have?" );
countArray[2] = input.nextInt();
System.out.println(countArray[0] + ", " + countArray[1] + ", " + countArray[2]);
for(int counter = 0; counter < countArray[0]; counter++){ //every exam score
System.out.printf("Enter Exam" + " " + (counter + 1) + " " + "score: ");
exams[counter]=input.nextInt();
System.out.println(exams[counter]);
}
System.out.println("----");
for(int counter = 0; counter < countArray[1]; counter++){ //every quiz score
System.out.printf("Enter Quiz" + " " + (counter + 1) + " " + "score: ");
quizzes[counter]=input.nextInt();
System.out.println(quizzes[counter]);
}
System.out.println("----");
for(int counter = 0; counter < countArray[2]; counter++){ //every lab score
System.out.printf("Enter Lab" + " " + (counter + 1) + " " + "score: ");
labs[counter]=input.nextInt();
System.out.println(labs[counter]);
}
System.out.println("----");
System.out.println("EXAMS:");
for(int x = 0; x < countArray[0]; x++)
System.out.println(exams[x]);
System.out.println("QUIZ:");
for(int x = 0; x < countArray[1]; x++)
System.out.println(quizzes[x]);
System.out.println("LABS:");
for(int x = 0; x < countArray[2]; x++)
System.out.println(labs[x]);
System.out.println("************************");
return countArray; //return back to calc grade
}
}
答案 0 :(得分:9)
您在调用中交换了参数。类型检查无法将您保存在此处。
也就是说,当你调用一个函数时
public static int [] getScores(int [] exams, int [] quizzes, int [] labs) {...}
带
int[] countarr = getScores(quizzes, exams, labs);
你不能指望结果有意义!
答案 1 :(得分:2)
签名与你称之为的参数之间存在不匹配。
int[] countarr = getScores(quizzes, exams, labs);
public static int [] getScores(int [] exams, int [] quizzes, int [] labs){
你显然混淆了论点