You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java学生成绩统计程序输出异常求助:首ID丢失、列错位

Java学生考试数据统计程序问题修复

问题描述

我正在开发一个统计12名学生考试数据的Java程序,功能是读取输入文件并将结果输出到文件,但遇到了几个问题:

  • 读取输入文件后无法打印首个学生ID;
  • 无论怎么调整格式化参数,标识对错题数量关系的C/I始终显示不到More Right/Wrong列;
  • <10无法显示在Omit列;
  • 表格列内容完全错位,修改%-10格式符也没用,预期各列内容对应显示,实际却混乱不堪。

原代码

import java.util.Scanner;
import java.io.*;

public class Topic3_HW3 {

    public static void main(String[] args) throws IOException {
        PrintWriter outputFile;
        outputFile = new PrintWriter("HW3_output.txt");
        // outputFile = new PrintWriter(System.out);
        // TODO Auto-generated method stub
        File myFile = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\Topic3_input.txt");
        Scanner inputFile = new Scanner(myFile);
        // print table header and columns
        outputFile.printf("%40s", "\tTable of Grades");
        outputFile.printf("\n%1s %7s %9s %12s %9s %10s %7s %12s %8s","\nStudent","ID","Correct","Incorrect","Total","More Right/Wrong","Omit","Grade","Percent");
        outputFile.println();
        // declare datatypes for different variables
        double pct;
        int grade, id, corr, incorr, total, omit;
        int student = 0;
        //prompt user to input initial student's ID
        System.out.print("Enter student's ID(negative 1 to stop): ");
        id = inputFile.nextInt();
        while (id != -1) {
            //prompt user to enter said student's wrong/right questions
            System.out.print("\nEnter the amount of questions the student answered correctly: ");
            corr = inputFile.nextInt();
            System.out.print("\nEnter the amount of questions the student answered incorrectly: ");
            incorr = inputFile.nextInt();
            student++;
            total = corr + incorr;
            // print error if total is more than 50 questions
            if (50 < total) {
            System.out.println("ERROR. TOTAL CANNOT EXCEED 50 QUESTIONS. PROCEEDING TO NEXT STUDENT.");}
            System.out.print("\nEnter the desired student's ID(negative 1 to stop): ");
            id = inputFile.nextInt();
            omit = 50 - (corr + incorr);
            grade = corr * 2;
            // calculate percentage correct. type cast correct and total to make sure it
            // doesnt truncate
            pct = (double) corr / total;
            //print if the omitted amount of questions is less, more, or equal to 10
            if (omit < 10)
                outputFile.printf("%-15s", "<10");
            if (10 < omit)
                outputFile.printf("%-15s", "10<");
            if (omit == 10)
                outputFile.printf("%-15s", "10");
            if (corr<incorr)
                outputFile.printf("%-10s","I");
            if(incorr<corr)
                outputFile.printf("%-10s","C");
            if (corr==incorr)
                outputFile.printf("%-10s","=");
            //print values for corresponding columns
            outputFile.printf("\n%1s%14s%7s%12s%12s%9s%11s%10.3f", student, id, corr, incorr, total, omit, grade, pct);
        }
        //notify viewer that program has run
        System.out.println("\nThe program is finished.");
        // flush buffer and close i/o files
        outputFile.flush();
        outputFile.close();
        inputFile.close();
    }
}

问题修复与优化

核心问题点

  1. 首个学生ID丢失:循环内先读取下一个ID再打印当前学生数据,导致当前学生的ID被覆盖。
  2. 列顺序混乱:分别打印Omit、More Right/Wrong内容后才换行打印其他列,完全不符合表格列的顺序。
  3. 格式化参数不匹配:表头与内容的列宽度不一致,导致对齐失败。
  4. 百分比计算错误:未乘以100,输出的是小数而非百分比值。
  5. 错误分支逻辑漏洞:总题数超50时,后续计算仍会执行,可能导致异常。

修正后的代码

import java.util.Scanner;
import java.io.*;

public class Topic3_HW3 {

    public static void main(String[] args) throws IOException {
        PrintWriter outputFile = new PrintWriter("HW3_output.txt");
        File myFile = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\Topic3_input.txt");
        Scanner inputFile = new Scanner(myFile);

        // 打印表头,统一列宽度
        outputFile.printf("%40s\n", "Table of Grades");
        outputFile.printf("%-8s %-8s %-10s %-12s %-8s %-18s %-8s %-8s %-10s\n",
                "Student", "ID", "Correct", "Incorrect", "Total", "More Right/Wrong", "Omit", "Grade", "Percent");

        double pct;
        int grade, id, corr, incorr, total, omit;
        int student = 0;

        // 读取第一个学生ID
        id = inputFile.nextInt();
        while (id != -1) {
            corr = inputFile.nextInt();
            incorr = inputFile.nextInt();
            student++;
            total = corr + incorr;

            // 总题数超50时跳过后续计算
            if (total > 50) {
                System.out.println("ERROR. TOTAL CANNOT EXCEED 50 QUESTIONS. PROCEEDING TO NEXT STUDENT.");
                // 读取下一个ID,进入下一次循环
                id = inputFile.nextInt();
                continue;
            }

            omit = 50 - total;
            grade = corr * 2;
            // 计算百分比(乘以100转为百分比格式)
            pct = (double) corr / total * 100;

            // 生成More Right/Wrong的标识
            String moreRightWrong;
            if (corr > incorr) {
                moreRightWrong = "C";
            } else if (corr < incorr) {
                moreRightWrong = "I";
            } else {
                moreRightWrong = "=";
            }

            // 生成Omit列的显示内容
            String omitStr;
            if (omit < 10) {
                omitStr = "<10";
            } else if (omit > 10) {
                omitStr = "10<";
            } else {
                omitStr = "10";
            }

            // 按表头顺序打印一行数据,统一对齐
            outputFile.printf("%-8d %-8d %-10d %-12d %-8d %-18s %-8s %-8d %-10.3f%%\n",
                    student, id, corr, incorr, total, moreRightWrong, omitStr, grade, pct);

            // 读取下一个学生ID
            id = inputFile.nextInt();
        }

        System.out.println("\nThe program is finished.");
        outputFile.close();
        inputFile.close();
    }
}

修复说明

  • 调整循环逻辑:先处理当前学生的所有数据,再读取下一个ID,避免首个学生ID被覆盖。
  • 统一列格式:表头和内容使用一致的%-X左对齐格式,保证每列宽度固定,内容对齐。
  • 合并打印逻辑:将所有列内容放在同一个printf语句中,严格对应表头顺序。
  • 修正百分比计算:乘以100并添加百分号,符合百分比显示习惯。
  • 完善错误分支:总题数超50时,跳过后续计算直接读取下一个ID,避免无效计算。

内容的提问来源于stack exchange,提问作者catdu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 06:47:51