打印正方形三角形。如何镜像数字?

Printing a squares triangle. How to mirror numbers?

因此,为了我的编程,我已经在这个实验室工作了一段时间 class,到目前为止,我认为我在正确的轨道上。

但是,我不太确定如何反映这些数字。差不多,我的代码只打印三角形的上半部分。无论如何,这是给我们的实际任务:

Write a program using a Scanner that asks the user for a number n between 1 and 9 (inclusive). The program prints a triangle with n rows. The first row contains only the square of 1, and it is right-justified. The second row contains the square of 2 followed by the square of 1, and is right justified. Subsequent rows include the squares of 3, 2, and 1, and then 4, 3, 2 and 1, and so forth until n rows are printed. Assuming the user enters 4, the program prints the following triangle to the console:

          1
       4  1
    9  4  1
16  9  4  1
    9  4  1
       4  1
          1

For full credit, each column should be 3 characters wide and the values should be right justified.

现在这是我到目前为止为我的代码编写的内容:

import java.util.Scanner;
public class lab6 {
    public static void main(String[] args) {
        Scanner kybd = new Scanner(System.in);
        System.out.println(
                "Enter a number that is between 1 and 9 (inclusive): ");

        // this is the value that the user will enter for # of rows
        int rows = kybd.nextInt();

        for (int i = rows; i > 0; i--) {
            for (int j = rows; j > 0; j--)
                System.out.print((rows - j + 1) < i ?
                        "   " : String.format("%3d", j * j));
            System.out.println();
        }
    }
}

这就是我输入 4:

时打印的代码
Enter a number that is between 1 and 9 (inclusive): 
4
          1
       4  1
    9  4  1
16  9  4  1

如你所见,我只能打印出三角形的上半部分。我一直在尝试弄清楚如何镜像它,但我似乎无法弄清楚。我在这个网站上和整个 Internet 上寻求帮助,但我似乎做不到。

答案是:

public static void main(String... args) {
    Scanner kybd = new Scanner(System.in);
    System.out.println("Enter a number that is between 1 and 9 (inclusive): ");

    int rows = kybd.nextInt(); // this is the value that the user will enter for # of rows

    for (int i = -rows + 1; i < rows; i++) {
        for (int j = -rows; j < 0; j++)
            System.out.print(abs(i) > j + rows ? "   " : String.format("%3d", j * j));
        System.out.println();
    }
}

尝试将其视为如何找到三个线性函数(位于两者之间的三角形面积)之间的点(笛卡尔):

y = 0 // in loops i is y and j is x
y = x + 4
y = -x -4

这里是 4 的示例结果:

和 9:

另一种选择:

public static void main(String args[]) {

    Scanner kybd = new Scanner(System.in);
    System.out.println("Enter a number that is between 1 and 9 (inclusive): ");
    int rows = kybd.nextInt(); // this is the value that the user will enter for # of rows

    int row = rows, increment = -1;
    while (row <= rows){
        for (int j = rows; j > 0; j--) {
            System.out.print(rows - j + 1 < row ? "   " : String.format("%3d", j * j));
        }
        System.out.println();
        if(row == 1) {
            increment = - increment;
        }
        row += increment;
    }
}

在外部 loopstream 你必须从 1-n 迭代到 n-1 (包括)并对负数取绝对值。其余同理。

如果n=6,那么三角形是这样的:

                 1
              4  1
           9  4  1
       16  9  4  1
    25 16  9  4  1
 36 25 16  9  4  1
    25 16  9  4  1
       16  9  4  1
           9  4  1
              4  1
                 1

Try it online!

int n = 6;
IntStream.rangeClosed(1 - n, n - 1)
        .map(Math::abs)
        .peek(i -> IntStream.iterate(n, j -> j > 0, j -> j - 1)
                // prepare an element
                .mapToObj(j -> i > n - j ? "   " : String.format("%3d", j * j))
                // print out an element
                .forEach(System.out::print))
        // start new line
        .forEach(i -> System.out.println());

另请参阅:Output an ASCII diamond shape using loops

1-nn-1的外循环,从n0的内递减循环。 if条件是i的绝对值不应该大于n - j.

Try it online!

int n = 6;
for (int i = 1 - n; i <= n - 1; i++) {
    for (int j = n; j > 0; j--)
        if (Math.abs(i) > n - j)
            System.out.print("   ");
        else
            System.out.printf("%3d", j * j);
    System.out.println();
}

输出:

                 1
              4  1
           9  4  1
       16  9  4  1
    25 16  9  4  1
 36 25 16  9  4  1
    25 16  9  4  1
       16  9  4  1
           9  4  1
              4  1
                 1

另请参阅: