P
P
PRAIT2020-10-31 09:50:53
Java
PRAIT, 2020-10-31 09:50:53

How to align an empty triangle?

Hello, the task is to draw an empty triangle.
Here is what he wrote

public class Test {
    public static void main(String[] args) {
        int count = 12;
        for (int i = 5; i < count; i++) {
            for (int k = 0; k < count; k++) {
                if (i == count - 1  || k == count - 1 - i || k == i + 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

It turned out a bit of a curve:
5f9d094919a77546037737.png

It should be like this:
5f9d0a8425587225859164.png

But the triangle turns out to be some kind of curve, something got confused. Where is the actual error in the code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2020-10-31
@PRAIT

It's only stretched because you have a stretched font in the console.
You have one problem - there is not enough asterisk one in the lower right corner (you need to fix k <= count).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question