0

我是我大学 Java 课程的助教,今天有一个学生在实验室遇到了一个非常奇怪的问题。我查看了大约一个小时,让实验室的其他 TA 做同样的事情,但我们找不到问题。

实际上,我们在这里所做的是创建 3 个数组,将它们传递给一个新方法。在新方法中修改这些数组的值并返回到原始方法。我们没有使用 return 语句将任何数组返回给原始方法。相反,我们正在利用,我只能将来自 C 背景的东西描述为传递引用。但是,在返回到原始方法时,值已更改为一些不正确的值。

在这个特定示例中,我们有三个数组,分别称为:“exams”、“quizzes”和“labs”。这些数组中的每一个的大小为 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
    }
    }
4

2 回答 2

9

你交换了调用中的参数。类型检查无法将您保存在这里。

也就是说,当你调用一个函数时

public static int [] getScores(int [] exams, int [] quizzes, int [] labs) {...}

int[] countarr = getScores(quizzes, exams, labs);

你不能指望结果有任何意义!

于 2013-10-11T20:21:33.693 回答
2

签名与您调用它的参数之间存在不匹配。

int[] countarr = getScores(quizzes, exams, labs);

public static int [] getScores(int [] exams, int [] quizzes, int [] labs){

你显然混淆了论点

于 2013-10-11T20:22:49.920 回答