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

Java保龄球计分表格格式化输出列对齐异常问题咨询

Java保龄球分数表格对齐问题排查与修复

我正在编写Java代码读取保龄球分数并打印规整表格,目标是让**Avg Score(平均分)和Avg Rating(平均评分)**列实现整齐对齐。已使用格式化输出替代\t或空格,但最后两列仍被前面的数据挤偏错位,现咨询问题原因及修复方法。

原代码及修复后代码如下:

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

public class Topic_HW5 {
    
    public static String oneGameScore(int x){
        String scr = " ";
        if (0<=x&&x<50) 
            scr = "Horrible";
        if (50<=x&&x<=99) 
            scr = "Poor";
        if (100<=x&&x<=139) 
            scr = "Good";
        if (140<=x&&x<=199) 
            scr = "Very Good";
        if (200<=x&&x<=249) 
            scr = "Excellent";
        if (250<=x&&x<=300) 
            scr = "Pro";
        if(x<0)
            scr= "Illegal <0";
        if(300<x)
            scr="Illegal <300";
        return scr;
    }
    
    public static int avg3Scores(int x, int y, int z) {
        return (x+y+z)/3;
    }

    public static boolean validGroup(int x,int y, int z) {
        return 0<=x&&x<=300 && 0<=y&&y<=300 && 0<=z&&z<=300;
    }
    
    public static void main(String[] args) throws IOException {
        PrintWriter outputFile= new PrintWriter("hw5out.txt");
        File myinputFile = new File("C:\\Users\\chroZ\\eclipse-workspace\\Topic3\\src\\hw5input.txt");
        Scanner inputFile = new Scanner(myinputFile);
        int score1, score2, score3,avg;
        boolean valid;
        String rtg,rtg2,rtg3,grpavg;
        
        // 统一表头和数据行的列宽度,确保对齐
        outputFile.printf("%50s%n", "TABLE OF BOWLING SCORES");
        // 为每列设置固定宽度,适配最长内容
        outputFile.printf("%-8s %-8s %-8s %-8s %-14s %-14s %-14s %-12s %-16s%n", 
                "Score 1", "Score 2", "Score 3", "Valid?", "Rating1", "Rating2", "Rating3", "Avg Score", "Avg Rating");
        
        System.out.println("What was score 1? ");
        score1 = inputFile.nextInt();
        while(score1 != -1) {
            System.out.println("What was score 2? ");
            score2 = inputFile.nextInt();
            System.out.println("What was score 3? ");
            score3 = inputFile.nextInt();
            
            valid=validGroup(score1,score2,score3);
            rtg=oneGameScore(score1);
            rtg2=oneGameScore(score2);
            rtg3=oneGameScore(score3);
            avg=avg3Scores(score1,score2,score3);
            grpavg=oneGameScore(avg);
            
            // 数据行使用与表头完全匹配的格式化宽度
            outputFile.printf("%-8d %-8d %-8d %-8b %-14s %-14s %-14s %-12d %-16s%n",
                    score1,score2,score3,valid,rtg,rtg2,rtg3,avg, grpavg);
            
            System.out.println("What was score 1? ");
            score1 = inputFile.nextInt();
        }
        
        outputFile.println("\nThe program is complete!");
        inputFile.close();
        outputFile.close();
    }
}

问题原因

  1. 表头与数据行宽度不匹配:原代码表头的列宽设置和数据行的格式化参数不一致,加上硬编码空格分隔,导致列的实际占用宽度混乱,后续列被前面溢出的内容挤偏。
  2. 列宽预留不足:部分评级内容(如Illegal <300)长度较长,原格式化宽度不足以容纳,内容溢出到相邻列。
  3. 布尔值列格式化不统一:原代码中%9b的宽度和表头Valid?的宽度不对应,进一步打乱整体对齐。

修复要点

  1. 统一列宽度:表头和数据行使用完全对应的格式化宽度(如%-8s对应%-8d、%-8b),确保每列固定宽度。
  2. 适配最长内容:针对最长的评级内容,将Rating列宽度设为14,避免内容溢出。
  3. 移除硬编码空格:用格式化宽度控制列间距,不再使用硬编码空格,保证对齐稳定性。
  4. 简化冗余逻辑:重构validGroup方法,用逻辑与判断替代嵌套if,代码更简洁。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 10:45:04