开发者

Drawing pentagon with asteriks on the console

I am drawing a triangle with asteriks on the console with for loops by taking the coordinates from the user.but I couldn't draw pentagon.pentagon includes 3 triangle but the program i wrote couldn't attach these triangles.It is drawing 3 differen开发者_运维知识库t triangle in different place on console.How can I solve this problem?Can you help me?


There are two possible approaches for this.

  1. Either you create an internal representation of the output (like a two-dimensional character array) in which you draw the graphics.

    When the image is done, you print the whole array.

  2. You use escape sequences (which are specific to your terminal) to move the cursor to a certain place to draw characters.

    In your case, you should clear the terminal once and then move the cursor for each asterisk.

[EDIT] As for #1:

char[][] screen = new char[20][]; // 20 lines
for(int i=0;i<screen.length; i++) screen[i] = new char[80]; // 80 columns

Now you can draw something with screen[y][x] = '*'

To print:

for(int i=0;i<screen.length; i++) System.out.print(new String(screen[i]));
System.out.println();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜