gpt4 book ai didi

java 数组返回错误值

转载 作者:行者123 更新时间:2023-12-01 06:39:08 25 4
gpt4 key购买 nike

我是大学 Java 类(class)的助教,今天在实验室,一名学生向我提出了一个非常奇怪的问题。我检查了大约一个小时,并让实验室的其他助教也做了同样的事情,但我们找不到问题。

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

在这个具体示例中,我们有三个数组,分别称为:“exams”、“quizzes”和“labs”。每个数组的大小均为 1,000,并初始化为 -1。在第一个方法“calcGrade”中,我们创建这些数组并初始化它们。然后,我们将所有三个数组传递给第二个方法,该方法捕获用户拥有的考试、测验和实验室数量,然后将实际的考试、测验和实验室成绩值存储到数组中。

方法 1(计算等级)

exams            quizzes           labs
-1 -1 -1
-1 -1 -1
-1 -1 -1
-1 -1 -1
. . .
. . .
. . .

方法 2(获取分数)

exams            quizzes           labs
90 80 90
-1 80 90
-1 -1 -1
-1 -1 -1
. . .
. . .
. . .

返回方法 1(计算成绩)

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
}
}

最佳答案

您交换了调用中的参数。类型检查救不了你。

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

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

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

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

关于java 数组返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19326562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com