【问题标题】:How to remove leading/trailing white spaces from a line of integers?如何从一行整数中删除前导/尾随空格?
【发布时间】:2016-01-08 08:40:09
【问题描述】:

我必须以类似矩阵的方式打印出表格,每个数字的宽度都设置为 4(这些数字是右对齐的,并去掉每行的前导/尾随空格)。前 3 行如下所示:

1   2   3   4   5   6   7   8   9  10  11  12
2   4   6   8  10  12  14  16  18  20  22  24
3   6   9  12  15  18  21  24  27  30  33  36

这是我的代码:

public static void main (String[] args) {
    int i,j;
    for(i=1;i<=3;i++){
        for(j=1;j<=12;j++){
            System.out.format("%4d",i*j);
        }
        System.out.println();
    }
}   

在输出中,第一个整数移动了 3 个空格。如何去除每行的前导/尾随空格?

【问题讨论】:

  • 代码看起来像 Java,但请在问题中说明您使用的是哪种语言。

标签: java formatting


【解决方案1】:

假设您想去掉中间所有无用的空格,为什么不首先避免它们呢?

public static void main(String[] args) {
    int rows = 3, columns = 12;

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= columns; j++) {
            // figure out the max # of digits needed
            int necessaryDigits;
            if (rows * j < 10) {
                necessaryDigits = 1;
            } else if (rows * j < 100) {
                necessaryDigits = 2;
            } else if (rows * j < 1000) {
                necessaryDigits = 3;
            } else {
                necessaryDigits = 4;
            }
            // print them accordingly with one extra space to distinguish
            // the numbers and avoid the leading one in 1st column
            System.out.format("%" + (necessaryDigits + (j == 1 ? 0 : 1))
                    + "d", i * j);
        }
        System.out.println();
    }
}

输出:

1 2 3  4  5  6  7  8  9 10 11 12
2 4 6  8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36

输出或 10 行:

 1  2  3  4  5  6  7  8  9  10  11  12
 2  4  6  8 10 12 14 16 18  20  22  24
 3  6  9 12 15 18 21 24 27  30  33  36
 4  8 12 16 20 24 28 32 36  40  44  48
 5 10 15 20 25 30 35 40 45  50  55  60
 6 12 18 24 30 36 42 48 54  60  66  72
 7 14 21 28 35 42 49 56 63  70  77  84
 8 16 24 32 40 48 56 64 72  80  88  96
 9 18 27 36 45 54 63 72 81  90  99 108
10 20 30 40 50 60 70 80 90 100 110 120

【讨论】:

  • 谢谢,我使用了你的想法,并根据问题做了一些不同的事情,结果成功了。
【解决方案2】:

尝试在第二个循环中打印与 i 相同的第一个数字,然后从第二个循环开始第二个循环,即 2,例如:

for(i=1;i<=3;i++){
    System.out.print(i);
    for(j=2;j<=12;j++){
         System.out.format("%4d",i*j);
     }
     System.out.println();
}

【讨论】:

  • 但是如果第一个循环被限制为 10 而不是 3,那么数字将不再正确对齐...
  • 它会......为什么不@xmoex?
  • 其实这个问题是在一个有竞争力的编程网站上给出的。我尝试提交与您相同的代码,但它通过了部分。这是一个如此简单的问题仍然让我发疯。任何其他建议表示赞赏
  • 第一个:数字之间还有空格,那它们呢?第二:如果第一个数字是10或以上,那么它不再与它上面的数字右对齐,因为它消耗两个字符空间而不是一个
【解决方案3】:

如果允许把你有这样的条件 整数 i, j;

for (i = 1; i <= 3; i++)
        {
            for (j = 1; j <= 12; j++)
            {
                if (j == 1)
                {
                    System.out.format("%d", i * j);
                }
                else
                 {
                    System.out.format("%4d", i * j);
                }
            }
            System.out.println();
        }

【讨论】:

    猜你喜欢
    • 2013-09-23
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多