简体   繁体   中英

Printing a squares triangle. How to mirror numbers?

So I've been working on this lab for a while now for my programming class and so far I think I'm on the right track.

However, I'm not quite sure how to mirror the numbers. So pretty much, my code is only printing the top half of the triangle. Anyway here is the actual assignment that was given to us:

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.

Now here is what I have written for my code so far:

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();
        }
    }
}

And this is what that code PRINTS when I enter 4 :

Enter a number that is between 1 and 9 (inclusive): 
4
          1
       4  1
    9  4  1
16  9  4  1

As you can see, I can only get the TOP half of the triangle to print out. I've been playing around trying to figure out how to mirror it but I can't seem to figure it out. I've looked on this website for help, and all over the Internet but I can't seem to do it.

Answer is:

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();
    }
}

Try think of this as how to find points(carthesians) that are betwean three linear functions(area of triangle that lied betwean):

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

And here is example result for 4:

在此处输入图片说明

And 9:

在此处输入图片说明

In the outer loop or stream you have to iterate from 1-n to n-1 (inclusive) and take absolute values for negative numbers. The rest is the same.

If n=6 , then the triangle looks like this:

                 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());

See also: Output an ASCII diamond shape using loops

Another alternative :

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;
    }
}

The outer loop from 1-n to n-1 inclusive, and the inner decrementing loop from n to 0 . The if condition is the absolute value of i should not be greater than 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();
}

Output:

                 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

See also: Invert incrementing triangle pattern

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM