gpt4 book ai didi

java - 从 Java 数组中获取输入变量

转载 作者:行者123 更新时间:2023-12-01 17:57:47 25 4
gpt4 key购买 nike

我正在尝试打印此数组中学生的姓名。我不断收到一条错误消息,内容为:

[Ljava.lang.String;@55f96302

我在顶部附近声明并初始化名称,并尝试使用“System.out.println(“学生“+Names+”的平均分数是:”+avg);”打印它。但显然,仅仅拥有名称并不是解决方案。我该如何修复我的代码?

import java.util.Scanner;

public class Grades {
//An array to hold the student names
String[] Names = new String[6];

//An array to hold students letter grades
char[] LetterGrades = new char[6];

//an array to hold each of the students test scores
double[] Test1Scores = new double[6];
double[] Test2Scores = new double[6];
double[] Test3Scores = new double[6];

void EnterData() {
//initialize the scanner to parse the user inputs
Scanner sc = new Scanner(System.in);

//initialize the for loop and create iterations from 0 to 6
for (int i = 0; i < 6; i++) {
//get user's name
System.out.print("Enter the name of student " + (i + 1) + ": ");
Names[i] = sc.next();

//Create a while loop to get each of the students test scores
do {
System.out.print("Enter the score for Test 1: ");
Test1Scores[i] = sc.nextDouble();
} while (Test1Scores[i] < 0 || Test1Scores[i] > 100);
do {
System.out.print("Enter the score for Test 2: ");
Test2Scores[i] = sc.nextDouble();
} while (Test2Scores[i] < 0 || Test2Scores[i] > 100);
do {
System.out.print("Enter the score for Test 3: ");
Test3Scores[i] = sc.nextDouble();
} while (Test3Scores[i] < 0 || Test3Scores[i] > 100);
System.out.println("Testscores: " + Test1Scores[i] + " " + Test2Scores[i] + " " + Test3Scores[i]);
}
}

void GetNames() {
for (int i = 0; i < 6; i++)
System.out.println((i + 1) + ". " + GetName(i + 1));
}

String GetName(int studNumber) {
return (Names[studNumber - 1]);
}
//prints the 3 test scores of the student
void GetTestScores(int studNumber) {
System.out.println(GetTestScore(studNumber, 1) + " ");
System.out.println(GetTestScore(studNumber, 2) + " ");
System.out.println(GetTestScore(studNumber, 3) + " ");
}

double GetTestScore(int studNumber, int testNumber) {
if (testNumber == 1)
return (Test1Scores[studNumber - 1]);
else if (testNumber == 2)
return (Test2Scores[studNumber - 1]);
else if (testNumber == 3)
return (Test3Scores[studNumber - 1]);
return 0;
}
//calculate the average test score
double GetAverageScore(int studNumber) {
double avg = (Test1Scores[studNumber - 1] + Test2Scores[studNumber - 1] + Test3Scores[studNumber - 1]) / 3;
System.out.println("The average scores of student "+Names+" is:"+avg);
return avg;
}

void GetLetterGrades() {
for (int i = 1; i <= 6; i++) {
System.out.println("The letter grade of student " + (i) + " is: " + GetLetterGrade(i));
}
}

char GetLetterGrade(int studNumber) {
double sum = Test1Scores[studNumber - 1] + Test2Scores[studNumber - 1] + Test3Scores[studNumber - 1];
char letterGrade;
if (sum / 3 >= 90)
letterGrade = 'A';
else if (sum / 3 >= 80)
letterGrade = 'B';
else if (sum / 3 >= 70)
letterGrade = 'C';
else if (sum / 3 >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}

void GetClassAverage() {
double sum = 0.0;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 3; j++)
sum += GetTestScore(i + 1, j + 1);
System.out.println("The average of the class is: " + sum / 18);
}

void GetTopGrade() {
char letterGrade;
double top = GetAverageScore(1);
for (int i = 2; i <= 6; i++)
if (GetAverageScore(i) > top)
top = GetAverageScore(i);
if (top >= 90)
letterGrade = 'A';
else if (top >= 80)
letterGrade = 'B';
else if (top >= 70)
letterGrade = 'C';
else if (top >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
System.out.println("The top score of the class is: " + letterGrade);
}
}

最佳答案

你要打印的是学生的名字,但变量Names是它的数组类型。

所以你要做的就是改变

System.out.println("The average scores of student "+Names+" is:"+avg);

进入

System.out.println("The average scores of student "+Names[studNumber]+" is:"+avg);

这个。

关于java - 从 Java 数组中获取输入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43245459/

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