如何在Java中编写非void方法输出选手评分汇总信息
Solution to Add Summary Output Method for Sports Events Program
Let's modify your existing code to include a non-void method that generates and returns a formatted summary of the contestant's scores. We'll also clean up some naming to follow standard Java conventions (camelCase for methods and variables) for better readability.
Modified Complete Code
import java.util.Scanner; public class SportsEvents { public static void main(String[] args) { String[] contestants = getContestants(); int[] scores = getScores(); // Call our new summary method and print the result String scoreSummary = getSummary(contestants, scores); System.out.println(scoreSummary); } public static String[] getContestants() { int numContestants = 1; String[] names = new String[numContestants]; Scanner ip = new Scanner(System.in); // Reuse scanner instead of creating inside loop for (int j = 0; j < numContestants; j++) { System.out.println("Enter contestant's name"); names[j] = ip.nextLine(); } ip.close(); // Clean up scanner resource return names; } public static int[] getScores() { int numScores = 8; int[] scores = new int[numScores]; Scanner ip = new Scanner(System.in); for (int a = 0; a < numScores; a++) { System.out.println("Enter score " + (a + 1) + ":"); // Add score number for clarity scores[a] = ip.nextInt(); } ip.close(); return scores; } public static int getMaxValue(int[] array) { int maxValue = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > maxValue) { maxValue = array[i]; } } return maxValue; } public static int getMinValue(int[] array) { int minValue = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] < minValue) { minValue = array[i]; } } return minValue; } public static int getAverage(int[] scores) { int sum = 0; for (int score : scores) { // Enhanced for-loop for cleaner code sum += score; } return sum / scores.length; } // New non-void method to generate formatted summary public static String getSummary(String[] contestants, int[] scores) { String contestantName = contestants[0]; int highestScore = getMaxValue(scores); int lowestScore = getMinValue(scores); int averageScore = getAverage(scores); // Build summary with StringBuilder for efficient string concatenation StringBuilder summary = new StringBuilder(); summary.append("=== Contestant Score Summary ===\n"); summary.append("Contestant Name: ").append(contestantName).append("\n"); summary.append("Highest Score: ").append(highestScore).append("\n"); summary.append("Lowest Score: ").append(lowestScore).append("\n"); summary.append("Average Score: ").append(averageScore).append("\n"); summary.append("================================"); return summary.toString(); } }
Key Changes Explained
- New
getSummaryMethod: This non-void method takes the contestants and scores arrays as inputs. It retrieves the contestant name, leverages your existing helper methods to calculate max/min/average, then builds a neatly formatted string and returns it. - Java Naming Conventions: Renamed methods like
getcontestantstogetContestantsand variables likenumcontestantstonumContestantsto align with industry standards. - Scanner Optimization: Reused a single Scanner instance per method instead of creating new ones inside loops, and closed them to prevent resource leaks.
- User-Friendly Input: Added score number prompts (e.g., "Enter score 1:") to make the input process clearer for the user.
Example Output
If you input the name James followed by scores 1 through 8, the program will output:
=== Contestant Score Summary === Contestant Name: James Highest Score: 8 Lowest Score: 1 Average Score: 4 ================================
内容的提问来源于stack exchange,提问作者Amadou Toure
相关产品推荐
相关产品推荐

