我正在尝试在此数组中打印学生的姓名。我不断收到一条错误消息,
[Ljava.lang.String; @ 55f96302
我声明并初始化顶部附近的名称,我正在尝试使用" System.out.println打印它("学生的平均分数" +姓名+"是:&#34 +平均值);"但显然单独拥有名称不是解决方案。我该如何修复我的代码?
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);
}
}
答案 0 :(得分:2)
您要打印的是学生的名字,但变量Names是它的数组类型。
所以你想做的就是改变
System.out.println("The average scores of student "+Names+" is:"+avg);
到
System.out.println("The average scores of student "+Names[studNumber]+" is:"+avg);
此。
答案 1 :(得分:1)
在您提供的示例中,您将打印数组本身而不是数组中的值。您需要遍历数组'Names'并单独打印出每个值。
for( int i = 0;i<Names.length;i++){
System.out.println(Names[i]);
}
答案 2 :(得分:1)
如果你想在字符串中打印学生姓名,例如学生a,b,c的平均分数是:100
您可以通过迭代元素并使用该方法创建一个返回输入String数组的逗号分隔值的方法: “学生的平均分数”+ csvStringMethod(姓名)+“是:”+ avg
答案 3 :(得分:0)
感谢您的帮助,但我能够使用以下内容:
System.out.println("The average scores of student " + Names[studentNumber - 1] + " is:"+avg);