この配列に生徒の名前を印刷しようとしています。私はLjava.lang.String [Javaの配列から入力変数を取得する
、というエラーメッセージが出続ける。55f96302
@私は宣言し、上部付近に名を初期化し、私は、「システムとそれを印刷しようとしています。 out.println( "学生の" +名前+ "の平均得点は" + 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);
}
}
私は名前を出力しますが、クラストップスコアが必要な箇所であるjava.lang.ArrayIndexOutOfBoundsException通知も表示されます。 – BWMustang13