Java中如何将for循环结果存为数组?如何优化给定判分代码?
Hey there! Let's break down your questions clearly, starting with storing for loop results in an array, then fixing and refactoring that student grading code.
How to Store For Loop Results in an Array in Java
Storing loop results in an array is straightforward—here's the standard workflow:
- Define the array size: First, figure out how many elements you need (usually based on the loop's iteration count).
- Initialize the array: Create an array of the appropriate type with that size.
- Assign values in the loop: Use the loop index to set each element of the array as you iterate.
For a simple example, if you wanted to store squares of numbers 0-9 in an array:
int[] squares = new int[10]; // Step 1 & 2: Array for 10 elements for (int i = 0; i < 10; i++) { squares[i] = i * i; // Step 3: Assign result to array index }
Refactoring the Student Grading Code
Your original code has two main issues: variable scope problems (the sum variables are defined inside the if block, so they can't be accessed outside) and redundant code (using separate variables for each student instead of a single array). Let's fix that with a clean, maintainable version:
Optimized Code
public class StudentGrading { public static void main(String[] args) { // Student answers (maintained as-is) char[][] answers = { { 'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' }, { 'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D' }, { 'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D' }, { 'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D' }, { 'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D' }, { 'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' }, { 'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D' }, { 'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D' }, }; // Answer key (maintained as-is) char[] key = { 'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D' }; // Array to store each student's score (one element per student) int[] studentScores = new int[answers.length]; // Calculate scores for each student for (int studentIdx = 0; studentIdx < answers.length; studentIdx++) { int correctAnswers = 0; // Check each question against the key for (int questionIdx = 0; questionIdx < key.length; questionIdx++) { if (answers[studentIdx][questionIdx] == key[questionIdx]) { correctAnswers++; } } // Store the calculated score in the array studentScores[studentIdx] = correctAnswers; } // Print all student results neatly for (int i = 0; i < studentScores.length; i++) { System.out.printf("Student %d got %d Marks.%n", i, studentScores[i]); } } }
Key Improvements Explained
- Fixed scope issues: We use a single array
studentScoresto store all scores, which is accessible throughout the method. No more scatteredsumvariables trapped in narrow scopes. - Eliminated redundancy: Instead of separate variables for each student, the array lets us handle any number of students dynamically.
- Flexible loop conditions: Replaced the hardcoded
j <= 9withkey.length—if the number of questions changes later, you won't need to update the loop logic. - Cleaner output: Used
printffor consistent, readable result formatting. - Better naming: Renamed the class to
StudentGrading(follows Java's camel-case naming convention) and used descriptive variable names likestudentIdxandcorrectAnswersfor readability.
内容的提问来源于stack exchange,提问作者nav2236
相关产品推荐
相关产品推荐

