关于使用Multidimensional Array实现学生成绩录入及平均分计算的咨询
Absolutely! Multidimensional Arrays Are Perfect for This Scenario
Using a multidimensional array is a totally straightforward, effective solution for your student grade tracking needs. Here's why it works and how to implement it:
Why This Approach Fits
A 2D array (which is what you're thinking of here) directly maps to your data structure:
- Each row represents a single student
- Each column represents one of the 5 grades
This structure makes it easy to input, store, and calculate averages for each student without messy data handling.
Step-by-Step Implementation (Example in Python)
Here's a concrete code example that matches your requirements:
# Get the number of students from input num_students = int(input("Enter number of students: ")) # Initialize a 2D array to hold all student grades student_grades = [] # Collect grades for each student for student_idx in range(num_students): print(f"\nEnter grades for Student {student_idx + 1} (5 subjects):") individual_grades = [] for grade_idx in range(5): grade = float(input(f"Grade {grade_idx + 1}: ")) individual_grades.append(grade) student_grades.append(individual_grades) # Calculate averages and format the table output print("\n" + "-" * 60) print(f"{'Student':<12} {'Grade 1':<10} {'Grade 2':<10} {'Grade 3':<10} {'Grade 4':<10} {'Grade 5':<10} {'Average':<10}") print("-" * 60) for idx, grades in enumerate(student_grades): avg_score = sum(grades) / len(grades) # Format each row to match your example print(f"Student{idx+1:<10} {grades[0]:<10.1f} {grades[1]:<10.1f} {grades[2]:<10.1f} {grades[3]:<10.1f} {grades[4]:<10.1f} {avg_score:<10.1f}") print("-" * 60)
Test Output (Matching Your Example)
If you input 2 students with the grades you provided, the output will look exactly like this:
------------------------------------------------------------ Student Grade 1 Grade 2 Grade 3 Grade 4 Grade 5 Average ------------------------------------------------------------ Student1 87.0 71.0 64.0 89.0 78.0 77.8 Student2 54.0 76.0 89.0 89.0 78.0 77.2 ------------------------------------------------------------
Key Notes
- This logic translates to any language (Java, C++, JavaScript, etc.)—the core idea of using a 2D array to map students to grades remains the same.
- The array keeps your data organized, making it simple to extend later (e.g., add more subjects, calculate class averages) if needed.
内容的提问来源于stack exchange,提问作者Sir Dog Man Thing
相关产品推荐
相关产品推荐

