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

Java实现可扩展多行数字右对齐格式化打印的代码需求

Solution for Aligned Dynamic Number Table in Java

Alright, let's tackle this problem where we need to generate a neatly aligned number table (like a multiplication table) without hardcoding print statements. The solution needs to support easy expansion to 10+ rows, handle three-digit numbers, and ensure all digits line up perfectly.

Core Approach

  1. Dynamic Column Width Calculation: First, figure out the maximum number in the table (which is rows * columns). The number of digits in this max value tells us how wide each column needs to be to keep everything aligned.
  2. Loop-Driven Generation: Use nested loops to generate each row and column value, instead of writing individual print statements.
  3. Formatted Output: Use String.format() to right-align each number in its column, ensuring consistent spacing regardless of digit count.
  4. Flexible Separation: Use a StringBuilder to construct each row, adding separators only between columns (not at the end).

Complete Code

public class AlignedNumberTable {
    public static void main(String[] args) {
        // Configure your table dimensions here - easy to expand to 10+ rows
        int totalRows = 5;
        int totalColumns = 5;
        
        // Calculate the largest number in the table to determine column width
        int maxNumber = totalRows * totalColumns;
        int columnWidth = String.valueOf(maxNumber).length();

        // Generate each row
        for (int row = 1; row <= totalRows; row++) {
            StringBuilder rowContent = new StringBuilder();
            for (int col = 1; col <= totalColumns; col++) {
                int currentValue = row * col;
                // Format the number to take up exactly columnWidth spaces (right-aligned)
                String formattedValue = String.format("%" + columnWidth + "d", currentValue);
                rowContent.append(formattedValue);
                
                // Add separator between columns (skip for last column)
                if (col < totalColumns) {
                    rowContent.append(" - ");
                }
            }
            // Print the fully constructed row
            System.out.println(rowContent.toString());
        }
    }
}

Key Explanations

  • Dynamic Column Width: By using String.valueOf(maxNumber).length(), we automatically adjust the column width to fit the largest number. For example, if we set totalRows = 10 and totalColumns = 10, the max number is 100 (3 digits), so each column will be 3 characters wide.
  • No Hardcoding: All table dimensions are stored in variables (totalRows, totalColumns). Just change these values to expand the table to 10 or more rows—no need to rewrite print logic.
  • Perfect Alignment: The %" + columnWidth + "d format specifier right-aligns each number in its column, so units, tens, and hundreds digits line up vertically across all rows.
  • Clean Separation: Using StringBuilder ensures we only add the "-" separator between columns, avoiding trailing separators at the end of each row.

Sample Output

For totalRows = 5 and totalColumns = 5, the output will be:

1 -  2 -  3 -  4 -  5
 2 -  4 -  6 -  8 - 10
 3 -  6 -  9 - 12 - 15
 4 -  8 - 12 - 16 - 20
 5 - 10 - 15 - 20 - 25

If we expand to totalRows = 10 and totalColumns = 10, the output will handle three-digit numbers seamlessly:

1 -  2 -  3 -  4 -  5 -  6 -  7 -  8 -  9 - 10
  2 -  4 -  6 -  8 - 10 - 12 - 14 - 16 - 18 - 20
  3 -  6 -  9 - 12 - 15 - 18 - 21 - 24 - 27 - 30
  4 -  8 - 12 - 16 - 20 - 24 - 28 - 32 - 36 - 40
  5 - 10 - 15 - 20 - 25 - 30 - 35 - 40 - 45 - 50
  6 - 12 - 18 - 24 - 30 - 36 - 42 - 48 - 54 - 60
  7 - 14 - 21 - 28 - 35 - 42 - 49 - 56 - 63 - 70
  8 - 16 - 24 - 32 - 40 - 48 - 56 - 64 - 72 - 80
  9 - 18 - 27 - 36 - 45 - 54 - 63 - 72 - 81 - 90
 10 - 20 - 30 - 40 - 50 - 60 - 70 - 80 - 90 - 100

内容的提问来源于stack exchange,提问作者J. Doe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:36:59