Java方法间数组传递与访问求助:火箭赛事统计开发
Hey there! Let's tackle your Rockets stats program step by step, especially focusing on how to pass arrays between methods since that's where you're stuck.
First, a quick key point to clear up: In Java, arrays are reference types. That means when you pass an array to a method, you're actually passing a reference to its memory location. So any changes you make to the array's elements inside the method will affect the original array in the calling method—no need to return a new array (though you can if you want to create a separate modified version).
Below is a complete, working example that includes two array-storing methods, plus all the stats calculations you need, with explanations for each part:
Full Code Example
import java.util.Scanner; public class MorenoJonathonRocketsStatistics { // Method 1: Store match data by getting input from the user public static void loadMatchDataFromUser(int[] rocketsScores, int[] opponentsScores) { Scanner scanner = new Scanner(System.in); int totalGames = rocketsScores.length; System.out.println("Enter " + totalGames + " games of Rockets scores vs opponent scores (separate each pair with a space):"); for (int i = 0; i < totalGames; i++) { System.out.print("Game " + (i + 1) + ": "); rocketsScores[i] = scanner.nextInt(); opponentsScores[i] = scanner.nextInt(); } scanner.close(); } // Method 2: Store pre-defined test match data (for quick testing) public static void loadTestMatchData(int[] rocketsScores, int[] opponentsScores) { // Simulate 10 games of data rocketsScores[0] = 104; opponentsScores[0] = 96; rocketsScores[1] = 98; opponentsScores[1] = 102; rocketsScores[2] = 112; opponentsScores[2] = 105; rocketsScores[3] = 89; opponentsScores[3] = 95; rocketsScores[4] = 120; opponentsScores[4] = 110; rocketsScores[5] = 92; opponentsScores[5] = 97; rocketsScores[6] = 108; opponentsScores[6] = 100; rocketsScores[7] = 85; opponentsScores[7] = 88; rocketsScores[8] = 115; opponentsScores[8] = 108; rocketsScores[9] = 90; opponentsScores[9] = 94; } // Calculate win percentage public static double calculateWinRate(int[] rocketsScores, int[] opponentsScores) { int wins = 0; for (int i = 0; i < rocketsScores.length; i++) { if (rocketsScores[i] > opponentsScores[i]) { wins++; } } return (double) wins / rocketsScores.length * 100; } // Calculate average point margin in losing games public static double calculateAvgLossMargin(int[] rocketsScores, int[] opponentsScores) { int losses = 0; int totalMargin = 0; for (int i = 0; i < rocketsScores.length; i++) { if (rocketsScores[i] < opponentsScores[i]) { losses++; totalMargin += opponentsScores[i] - rocketsScores[i]; } } return losses == 0 ? 0 : (double) totalMargin / losses; } // Find lowest Rockets score and the corresponding game number public static void findLowestScore(int[] rocketsScores) { int lowest = rocketsScores[0]; int gameNum = 1; for (int i = 1; i < rocketsScores.length; i++) { if (rocketsScores[i] < lowest) { lowest = rocketsScores[i]; gameNum = i + 1; } } System.out.println("Lowest Rockets score: " + lowest + " (Game " + gameNum + ")"); } // Main method to tie everything together public static void main(String[] args) { int totalGames = 10; // Adjust this number to change how many games you're tracking // Initialize arrays to hold score data int[] rocketsScores = new int[totalGames]; int[] opponentsScores = new int[totalGames]; // Choose to load test data or user input (uncomment the one you want) // loadMatchDataFromUser(rocketsScores, opponentsScores); loadTestMatchData(rocketsScores, opponentsScores); // Run calculations and print results double winRate = calculateWinRate(rocketsScores, opponentsScores); System.out.printf("Rockets Win Rate: %.2f%%\n", winRate); double avgLossMargin = calculateAvgLossMargin(rocketsScores, opponentsScores); System.out.printf("Average Loss Margin: %.2f\n", avgLossMargin); findLowestScore(rocketsScores); } }
Key Explanations
- Array Passing: Notice how
loadMatchDataFromUserandloadTestMatchDatatake arrays as parameters and modify their elements directly. Since arrays are reference types, these changes are reflected in the original arrays created in themainmethod—no need to return the arrays. - Modular Methods: Each stats calculation is split into its own method to keep code clean and reusable. For example,
calculateWinRateonly handles win percentage, so you can easily modify or reuse it later. - Flexibility: You can swap between user input and test data by commenting/uncommenting the respective method calls. Adjust
totalGamesto track more or fewer matches.
内容的提问来源于stack exchange,提问作者user9424437

