3

I wrote the code to get the following formatted output, but when I enter number of rows in double digits, the output format changes. Why? How can I fix this?

      1
    1 2 1
  1 2 3 2 1
1 2 3 4 3 2 1

Here is my code:

import java.util.*;
class PTri {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the no. of rows for which " +
                "triangle has to be constructed");
        int numrow = sc.nextInt();
        for (int i = 1; i <= numrow; i++) {
            for (int j = 1; j <= numrow - i; j++) {
                System.out.print("  ");
            }
            for (int k = 1; k < i * 2; k++) {
                System.out.print(Math.min(k, i * 2 - k) + " ");
            }
            System.out.println();
        }
    }
}
4

3 回答 3

2

这是因为两位数的值会改变整个架构。集合将向右移动一个位置。所以你可以提出这样的条件。我在数字之间添加了一个额外的空格以提高可见性。

import java.util.*;
class PTri {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the no. of rows for which " +
                "triangle has to be constructed");
        int numrow = sc.nextInt();
        for (int i = 1; i <= numrow; i++) {
            for (int j = 1; j <= (numrow - i); j++) {
                System.out.print("    ");
            }
            for (int k = 1; k < i * 2; k++) {
                int temp = Math.min(k, i * 2 - k);

                if (temp > 9) {
                    System.out.print(temp + "  ");
                } else {
                    System.out.print(temp + "   ");
                }
            }
            System.out.println();
        }
    }
}
于 2012-06-07T15:55:54.053 回答
1

在这个例子中,我计算了数字,并为每个数字添加了一个额外的空格。值的输出使用前导零(数字计数)进行格式化。

public static void main(final String[] args) {
    final Scanner sc = new Scanner(System.in);
    System.out.println("Enter the no. of rows for which " +
            "triangle has to be constructed");
    final int numrow = 100;// sc.nextInt();

    final int digits = (int) Math.log10(numrow) + 1;

    for (int i = 1; i <= numrow; i++) {
        for (int j = 1; j <= numrow - i; j++) {
            System.out.print(" ");
            for (int l = 0; l < digits; l++) {
                System.out.print(" ");
            }
        }
        for (int k = 1; k < i * 2; k++) {
            final int value = Math.min(k, i * 2 - k);
            System.out.print(String.format("%0" + digits + "d ", value));
        }
        System.out.println();
    }
}
于 2012-06-07T16:16:23.690 回答
0

您可以使用String.format方法:

  • "%2d"- 格式为两位数
  • "%02d"- 格式为带有前导零的两位数。

例子:

// int n = 5;
int n = 12;
// number of digits
int digits = String.valueOf(n).length();
// format string
String format = "%" + digits + "d";
// output
System.out.println("n=" + n + ", format=" + format);
IntStream.rangeClosed(1, n)
        .mapToObj(i -> IntStream.rangeClosed(-n, i)
                .map(Math::abs)
                .map(j -> j = i - j)
                .filter(j -> j != 0)
                .mapToObj(j -> j > 0 ?
                        String.format(format, j) : " " .repeat(digits))
                .collect(Collectors.joining(" ")))
        .forEach(System.out::println);

输出:

n=5, format=%1d
        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
n=12, format=%2d
                                  1
                               1  2  1
                            1  2  3  2  1
                         1  2  3  4  3  2  1
                      1  2  3  4  5  4  3  2  1
                   1  2  3  4  5  6  5  4  3  2  1
                1  2  3  4  5  6  7  6  5  4  3  2  1
             1  2  3  4  5  6  7  8  7  6  5  4  3  2  1
          1  2  3  4  5  6  7  8  9  8  7  6  5  4  3  2  1
       1  2  3  4  5  6  7  8  9 10  9  8  7  6  5  4  3  2  1
    1  2  3  4  5  6  7  8  9 10 11 10  9  8  7  6  5  4  3  2  1
 1  2  3  4  5  6  7  8  9 10 11 12 11 10  9  8  7  6  5  4  3  2  1

另请参阅:在每行之后打印二维数组中的行和列的总和

于 2021-02-21T11:32:28.857 回答