Java数组开发求助:学生成绩管理功能实现遇阻
Hey there! Let's work through your student grade management program issues step by step. I noticed a few key bugs in your current code, plus we need to adjust things to show student details alongside the highest and lowest scores. Let's break this down:
First off, your input logic has a common Scanner issue: when you use console.nextInt() to read the student ID or grade, it doesn't consume the newline character after the number. This causes the subsequent console.nextLine() to read an empty string for the student name. We can fix this by adding an extra console.nextLine() after each nextInt() to "eat" that leftover newline. Also, Java class names can't have spaces — Final project needs to be FinalProject.
Here's the corrected input loop:
for (int i = 0; i < 5; i++) { System.out.println("\nStudent Number " + (i + 1)); System.out.print("Student ID Number: "); studentID[i] = console.nextInt(); console.nextLine(); // Clear the newline after the ID System.out.print("Student Name: "); studentName[i] = console.nextLine(); // Now correctly reads the full name System.out.print("Grade: "); studentScore[i] = console.nextInt(); }
Your original printLowest and printHighest methods had reversed logic (they were finding the opposite value!) and only returned the score, not the associated student info. Instead of just returning a score, let's create helper methods that return the index of the highest/lowest score in the array. Using this index, we can pull the matching ID and name from your other arrays.
Add these helper methods:
public static int findHighestScoreIndex(int[] scores) { int highestIndex = 0; for (int i = 1; i < scores.length; i++) { if (scores[i] > scores[highestIndex]) { highestIndex = i; } } return highestIndex; } public static int findLowestScoreIndex(int[] scores) { int lowestIndex = 0; for (int i = 1; i < scores.length; i++) { if (scores[i] < scores[lowestIndex]) { lowestIndex = i; } } return lowestIndex; }
Now use these index methods in your main function to display all the required info:
// After collecting all student data int highestIndex = findHighestScoreIndex(studentScore); System.out.println("\n=== Highest Score ==="); System.out.println("Student ID: " + studentID[highestIndex]); System.out.println("Student Name: " + studentName[highestIndex]); System.out.println("Score: " + studentScore[highestIndex]); int lowestIndex = findLowestScoreIndex(studentScore); System.out.println("\n=== Lowest Score ==="); System.out.println("Student ID: " + studentID[lowestIndex]); System.out.println("Student Name: " + studentName[lowestIndex]); System.out.println("Score: " + studentScore[lowestIndex]);
Here's the complete, working version of your program with all fixes included:
import java.util.*; public class FinalProject { public static void main(String args[]) { Scanner console = new Scanner(System.in); int[] studentID = new int[5]; String[] studentName = new String[5]; int[] studentScore = new int[5]; for (int i = 0; i < 5; i++) { System.out.println("\nStudent Number " + (i + 1)); System.out.print("Student ID Number: "); studentID[i] = console.nextInt(); console.nextLine(); // Clear newline after ID input System.out.print("Student Name: "); studentName[i] = console.nextLine(); System.out.print("Grade: "); studentScore[i] = console.nextInt(); } // Display highest score details int highestIndex = findHighestScoreIndex(studentScore); System.out.println("\n=== Highest Score ==="); System.out.println("Student ID: " + studentID[highestIndex]); System.out.println("Student Name: " + studentName[highestIndex]); System.out.println("Score: " + studentScore[highestIndex]); // Display lowest score details int lowestIndex = findLowestScoreIndex(studentScore); System.out.println("\n=== Lowest Score ==="); System.out.println("Student ID: " + studentID[lowestIndex]); System.out.println("Student Name: " + studentName[lowestIndex]); System.out.println("Score: " + studentScore[lowestIndex]); } public static int findHighestScoreIndex(int[] scores) { int highestIndex = 0; for (int i = 1; i < scores.length; i++) { if (scores[i] > scores[highestIndex]) { highestIndex = i; } } return highestIndex; } public static int findLowestScoreIndex(int[] scores) { int lowestIndex = 0; for (int i = 1; i < scores.length; i++) { if (scores[i] < scores[lowestIndex]) { lowestIndex = i; } } return lowestIndex; } }
- Input Fix: The extra
console.nextLine()calls ensure that we don't get empty strings for student names, which was probably part of your "Step 2" frustration. - Index-Based Lookup: Returning the index instead of just the score lets us easily fetch the matching student ID and name, which solves your Step 3 and 4 requirements.
- Method Clarity: Renaming methods to
findHighestScoreIndexmakes their purpose obvious, avoiding the reversed logic issue from your original code.
内容的提问来源于stack exchange,提问作者EvilDinosaur312

