我正在尝试为初学者 Java 类编写一个具有规范的程序。代码如下。有一行没有在第一个内部编译大约 30 行,以便查看附近的第一个 do while 循环。
分数[i + 1][i] = input.nextDouble();
这正是这行代码。它不会编译,也不会给出任何错误,所以我不知道我做错了什么。我现在有点茫然,可以用第二双眼睛。只是为了解释我做错了什么,以便我可以修复它以及我应该如何识别它。
这些是本作业的规格 ******************************************* ******************************。菜单系统可以选择输入下一个学生的成绩。按下后,用户将输入该学生参加了多少次考试。然后程序将要求用户输入每个考试成绩。菜单将有一个选项显示学生的考试平均成绩。菜单将具有按考试显示考试平均值的选项。菜单将有一个选项来显示所有考试的当前班级平均值。****************************************************** ***********************。
import java.util.*;
public class JaggedArray {
public static void main (String[ ] args) {
int numStudents = 0, count = 0, x = 7;
int numGrades = 0;
String choice = "";
int vId[];
double score[ ][ ] = null;
Scanner input = new Scanner (System.in);
System.out.println("Welcome. How many students do you want to record grades for today?");
numStudents = input.nextInt();
vId = new int[numStudents];
for (int s = 0; s < vId.length; s++) {
System.out.println("Please enter the students class ID.");
vId [s] = input.nextInt();
System.out.println("How many exam grades would you like to record for this student?");
numGrades = input.nextInt();
do {
System.out.println("Enter all exams taken for this student.");
for (int i = 0; i < numGrades; i++) {
System.out.print("\tSTUDENT # " + (count + 1) + " \tEXAM # " + (i + 1) + " : ");
score[i + 1][i] = input.nextDouble();
}
count++;
System.out.println("Is there additional students you would like to add? (y/n) : ");
input.nextLine().toLowerCase();
choice = input.nextLine();
} while (choice.equals("y"));
}
//menu loop
do {
try { //will try and catch to prevent input error by user, sets x to different value if correct
System.out.println("*****************MENU*****************");
System.out.println("1.\t Display the average of all exams by each Student ID.");
System.out.println("2.\t Display the average of each exam by Exam #.");
System.out.println("3.\t Display the current class average for all exams for all students.");
System.out.println("**************************************");
System.out.println("\n");
System.out.print("Please enter your choice: ");
int menuOption = input.nextInt();
//average of all exams by student
if (menuOption == 1) {
for (int i = 0; i < count; i++) {
int studentSum = 0;
for (int j = 0; j < numGrades; j++) {
studentSum += score[i][j];
} //end of inner for loop
double studentAvg = (double) studentSum / numGrades;
System.out.println("\nStudent #" + (i + 1) + " : " + studentAvg + "\n");
} //end of outer for loop
//average of all exams by exam #
} else if (menuOption == 2) {
for (int i = 0; i < numGrades; i++) {
int examSum = 0;
for (int j = 0; j < count; j++) {
examSum += score[j][i];
} //end of inner for loop
double examAvg = (double) examSum / numGrades;
System.out.println("\nExam #" + (i + 1) + " : " + examAvg + "\n");
} //end of outer for loop
//current class average for all exams for all students
} else if (menuOption == 3) {
int classSum = 0;
for (int i = 0; i < count; i++) {
for (int j = 0; j < numGrades; j++) {
classSum += score[j][i];
} //end of inner for loop
} //end of outer for loop
double classAvg = (double) classSum / (numGrades * count);
System.out.println("\nAverage : " + classAvg + "\n");
} else {
System.out.println("Invalid choice");
} //end of if-else statement
//ask to select from menu again
System.out.print("Would you like to continue (y/n) : ");
input.nextLine().toLowerCase();
choice = input.nextLine();
x = 1;
} // end try and on to catch
catch (Exception e) {
System.out.println("Sorry, you can't do that. Try again.");
} // end catch
} // end do-while
while (choice.equals("y") && (x == 1));
System.out.println("Thank you!");
} //end of main
} //end of class