如何格式化Java控制台输出?自制PC应用收据排版优化
如何规整PC组装控制台程序的收据输出格式
我开发了一款支持自定义组装PC的简易控制台应用,最终会打印包含所选配件的收据,但目前输出的收据排版不规整。以下是现有代码及期望的规整排版样式,请问如何实现该格式化效果?
现有代码
public static void printReceipt() { System.out.println("================================="); System.out.println(" Your Receipt "); System.out.println("================================="); System.out.println("1) " + mbName + " " + mbPrice); System.out.println("2) " + proName + " " + proPrice); System.out.println("8) " + gpuName + " " + gpuPrice); System.out.println("3) " + ramName + " " + ramPrice); System.out.println("4) " + stoName + " " + stoPrice); System.out.println("5) " + coolName + " " + coolPrice); System.out.println("6) " + psuName + " " + psuPrice); System.out.println("7) " + cabName + " " + cabPrice); double total = subtotal(mbPrice, proPrice, ramPrice, stoPrice, coolPrice, psuPrice, cabPrice, gpuPrice); System.out.println("================================="); System.out.println("GRAND TOTAL: " + total + "/-"); System.out.println("================================="); System.out.println(" ================================="); System.out.println("| Thank you |"); System.out.println(" ================================="); }
期望排版样式
================================= Your Receipt ================================= Item Price ------ ------- 1) Asus 20000.0 2) INTEL 45000.0 8) Nvidia gtx 1050ti 17000.0 3) Asus 18000.0 4) Samsung 20000.0 5) Coolermaster 20000.0 6) Ortis 4000.0 7) Asus 4000.0 ================================= GRAND TOTAL: 148000.0/- =================================
解决方案
核心是使用Java的System.out.printf()方法,通过格式化占位符控制文本的对齐方式和宽度,实现规整排版:
- 表头与分隔线:保留原有顶部分隔线和标题,新增
Item和Price表头,用格式化保证两者对齐。 - 配件行格式化:用
"%2d) %-20s %10.1f%n"这类占位符:%2d:序号占2个字符宽度,右对齐(确保1)、8)这类序号对齐)%-20s:配件名占20个字符宽度,左对齐(短名称自动补空格,长名称超出会自动换行,可按需调整宽度)%10.1f:价格占10个字符宽度,右对齐,保留1位小数(可调整小数位数)
- 总计行格式化:同样用占位符固定
GRAND TOTAL:的宽度,让总价对齐到右侧。
修改后的完整代码
public static void printReceipt() { System.out.println("================================="); System.out.println(" Your Receipt "); System.out.println("================================="); // 打印表头 System.out.printf("%n %-20s %10s%n", "Item", "Price"); System.out.printf(" %-20s %10s%n", "------", "-------"); System.out.println(); // 格式化打印各配件 System.out.printf("%2d) %-20s %10.1f%n", 1, mbName, mbPrice); System.out.printf("%2d) %-20s %10.1f%n", 2, proName, proPrice); System.out.printf("%2d) %-20s %10.1f%n", 8, gpuName, gpuPrice); System.out.printf("%2d) %-20s %10.1f%n", 3, ramName, ramPrice); System.out.printf("%2d) %-20s %10.1f%n", 4, stoName, stoPrice); System.out.printf("%2d) %-20s %10.1f%n", 5, coolName, coolPrice); System.out.printf("%2d) %-20s %10.1f%n", 6, psuName, psuPrice); System.out.printf("%2d) %-20s %10.1f%n", 7, cabName, cabPrice); double total = subtotal(mbPrice, proPrice, ramPrice, stoPrice, coolPrice, psuPrice, cabPrice, gpuPrice); System.out.println("================================="); // 格式化打印总计 System.out.printf("%-20s %10.1f/-%n", "GRAND TOTAL:", total); System.out.println("================================="); System.out.println(" ================================="); System.out.println("| Thank you |"); System.out.println(" ================================="); }
说明:
- 可根据实际需求调整占位符宽度(比如把
%-20s改成%-25s让名称显示更长) - 如果价格不需要小数,可把
%10.1f改成%10.0f(适配double类型)或%10d(适配int类型)
内容的提问来源于stack exchange,提问作者AnuragK
相关产品推荐
相关产品推荐

