编写Java程序:录入5名学生百分制分数并生成表格及总分
Java Program to Record Student Scores & Generate Summary Table
Hey there! Let's build out your Java program to handle inputting scores for 5 students, display a formatted table, and calculate the total score. Here's the complete implementation along with clear explanations:
Complete Code
import java.util.Scanner; public class test33 { public static void main(String[] args) { Scanner kbReader = new Scanner(System.in); final int NUMBER_OF_STUDENTS = 5; int[] scores = new int[NUMBER_OF_STUDENTS]; // Step 1: Prompt user to enter scores for each student for (int i = 0; i < NUMBER_OF_STUDENTS; i++) { System.out.printf("Enter score for Student No #%d (out of 100): ", i + 1); scores[i] = kbReader.nextInt(); } // Step 2: Generate the summary table System.out.println("\n--- Student Score Summary ---"); // Print table headers with aligned columns System.out.printf("%-15s %-20s%n", "Student No #", "Score(out of 100)"); System.out.println("-----------------------------"); // Print each student's data and calculate total score int totalScore = 0; for (int i = 0; i < NUMBER_OF_STUDENTS; i++) { System.out.printf("%-15d %-20d%n", i + 1, scores[i]); totalScore += scores[i]; } // Display the total score System.out.println("-----------------------------"); System.out.printf("%-15s %-20d%n", "Total", totalScore); kbReader.close(); } }
Key Breakdown of the Code
- Input Collection: We use a
forloop to cycle through each student, prompting for their score and storing it in an array. TheNUMBER_OF_STUDENTSconstant makes it easy to adjust the number of students later if needed. - Clean Table Formatting:
printfis used with format specifiers like%-15sto ensure left-aligned, fixed-width columns—this makes the table look organized and easy to read. - Total Score Calculation: As we print each student's score, we accumulate the total in a variable, then display it at the bottom of the table for a quick summary.
- Resource Management: We close the
Scannerat the end to avoid unnecessary resource leaks, which is a good practice for I/O classes.
Example Output
If you input scores 55, 66, 77, 88, 99, the output will look like this:
Enter score for Student No #1 (out of 100): 55 Enter score for Student No #2 (out of 100): 66 Enter score for Student No #3 (out of 100): 77 Enter score for Student No #4 (out of 100): 88 Enter score for Student No #5 (out of 100): 99 --- Student Score Summary --- Student No # Score(out of 100) ----------------------------- 1 55 2 66 3 77 4 88 5 99 ----------------------------- Total 385
Optional Enhancement: Input Validation
If you want to ensure users only enter valid scores (0-100), you can modify the input loop to add a check:
for (int i = 0; i < NUMBER_OF_STUDENTS; i++) { int score; do { System.out.printf("Enter score for Student No #%d (out of 100): ", i + 1); score = kbReader.nextInt(); if (score < 0 || score > 100) { System.out.println("Invalid score! Please enter a value between 0 and 100."); } } while (score < 0 || score > 100); scores[i] = score; }
This prevents invalid entries and guides the user to input correct values.
内容的提问来源于stack exchange,提问作者عبدالله عبود
相关产品推荐
相关产品推荐

