Java程序:printHighest与printLowest方法返回多字段问题求助
Fixing
printHighest and printLowest to Return Full Student Details Hey there! Let's get your methods working to return the full student info (ID, name, score) instead of just the raw score. We'll also clean up a couple of other bugs in your code along the way.
First, Let's Spot the Existing Issues
- Method Name Mismatch: Your
printLowestmethod actually finds the highest score, andprintHighestfinds the lowest. That's backwards and will confuse anyone reading the code! - No Access to Student Metadata: Right now, your methods only take the
studentScorearray—there's no way to link the score back to the corresponding student ID or name. - Broken Roster Sorting: In
printRoaster, you're sorting each array individually. This breaks the connection between a student's ID, name, and score (e.g., a student's ID might end up paired with another student's name after sorting).
Fix 1: Update Methods to Use All Student Data (Array-Based Approach)
We'll modify printHighest and printLowest to accept all three arrays, find the index of the min/max score, then return a string with the full student details. We'll also fix the method names to match their actual purpose.
Here's the revised code:
import java.util.*; public class FinalprojectV2 { 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("Student Number " + (i + 1)); System.out.println("Student ID Number: "); studentID[i] = console.nextInt(); console.nextLine(); // Fix: consume the leftover newline after nextInt() System.out.println("Student Name: "); studentName[i] = console.nextLine(); System.out.println("Grade: "); studentScore[i] = console.nextInt(); } printRoaster(studentID, studentName, studentScore); // Get and print highest score details String highestDetails = getHighestStudentDetails(studentID, studentName, studentScore); System.out.println("Highest score student: " + highestDetails); // Get and print lowest score details String lowestDetails = getLowestStudentDetails(studentID, studentName, studentScore); System.out.println("Lowest score student: " + lowestDetails); } public static void printRoaster(int[] studentID, String[] studentName, int[] studentScore) { System.out.println("\nStudent Roster:"); for (int i = 0; i < 5; i++) { System.out.println(studentID[i] + " " + studentName[i] + " " + studentScore[i]); } } public static String getHighestStudentDetails(int[] studentID, String[] studentName, int[] studentScore) { int maxIndex = 0; for (int i = 1; i < studentScore.length; i++) { if (studentScore[i] > studentScore[maxIndex]) { maxIndex = i; } } // Return formatted string with all student info return studentID[maxIndex] + " | " + studentName[maxIndex] + " | Score: " + studentScore[maxIndex]; } public static String getLowestStudentDetails(int[] studentID, String[] studentName, int[] studentScore) { int minIndex = 0; for (int i = 1; i < studentScore.length; i++) { if (studentScore[i] < studentScore[minIndex]) { minIndex = i; } } // Return formatted string with all student info return studentID[minIndex] + " | " + studentName[minIndex] + " | Score: " + studentScore[minIndex]; } }
Key Changes in This Fix:
- Fixed Input Handling: Added
console.nextLine()afternextInt()to consume the leftover newline character, which was causing issues with reading student names. - Renamed Methods: Changed
printHighest/printLowesttogetHighestStudentDetails/getLowestStudentDetailsto better reflect what they do, and fixed their logic to find the correct min/max. - Passed All Arrays: Methods now receive
studentID,studentName, andstudentScoreso they can link scores to the right student. - Returned Full Details: Methods return a formatted string containing the student's ID, name, and score instead of just an integer.
Fix 2: Better Approach - Use a Student Class (Object-Oriented)
For cleaner, more maintainable code, we should create a Student class to hold all a student's data in one place. This avoids the risk of array misalignment entirely.
Here's the improved version:
import java.util.*; class Student { private int id; private String name; private int score; public Student(int id, String name, int score) { this.id = id; this.name = name; this.score = score; } // Getters to access private fields public int getId() { return id; } public String getName() { return name; } public int getScore() { return score; } // For easy printing @Override public String toString() { return id + " | " + name + " | Score: " + score; } } public class FinalprojectV2 { public static void main(String args[]) { Scanner console = new Scanner(System.in); List<Student> students = new ArrayList<>(); for (int i = 0; i < 5; i++) { System.out.println("Student Number " + (i + 1)); System.out.println("Student ID Number: "); int id = console.nextInt(); console.nextLine(); System.out.println("Student Name: "); String name = console.nextLine(); System.out.println("Grade: "); int score = console.nextInt(); students.add(new Student(id, name, score)); } printRoaster(students); Student highestStudent = getHighestStudent(students); System.out.println("\nHighest score student: " + highestStudent); Student lowestStudent = getLowestStudent(students); System.out.println("Lowest score student: " + lowestStudent); } public static void printRoaster(List<Student> students) { System.out.println("\nStudent Roster:"); // Optional: Sort the roster by score (or ID/name) students.sort(Comparator.comparingInt(Student::getScore)); for (Student student : students) { System.out.println(student); } } public static Student getHighestStudent(List<Student> students) { Student highest = students.get(0); for (Student student : students) { if (student.getScore() > highest.getScore()) { highest = student; } } return highest; } public static Student getLowestStudent(List<Student> students) { Student lowest = students.get(0); for (Student student : students) { if (student.getScore() < lowest.getScore()) { lowest = student; } } return lowest; } }
Why This Is Better:
- No Data Misalignment: All student data is stored in a single object, so you never have to worry about IDs/names/scores getting out of sync when sorting or modifying data.
- Cleaner Code: Methods work with
Studentobjects directly, making the code easier to read and maintain. - Flexibility: You can easily add more fields (like email, major) to the
Studentclass later without changing the rest of your code.
内容的提问来源于stack exchange,提问作者EvilDinosaur312
相关产品推荐
相关产品推荐

