简体   繁体   中英

Draws a Xmas tree on the console, using java.util.Scanner to input size, asking about where the spaces go

I've been trying draw a Xmas tree (print to console) by using size which is input from user.

I also have been trying to find out the logic such as spaces would it take . Any hint for me would be fantastic. Also, this lab requires only one for loop and have to call the method write-pattern which I provide below. Thank you so much.

Sorry if the code was too long. It's just to make sure that you guys can understand it.

Here is the output that I should display with input 4:所需显示

for (int i=0; i <=size; i++) {
        writePattern (' ','*',' ', ' ',' ',size -i,i,0,0,0,size+2);
        writePattern (' ','*','*',' ',' ',size -i,i,i,0,0,size+2);
    }

This is the wrirePattern that I mentioned above.

private void writePattern(char ch1, char ch2, char ch3, char ch4, char ch5, int n1, int n2, int n3, int n4, int n5, int length)  

{

while ((n1 > 0) && (length > 0)) {
    System.out.print(ch1);
    n1--;
    length--;
     }

 while ((n2 > 0) && (length > 0)) {
    System.out.print(ch2);
    n2--;
    length--;
 }

 while ((n3 > 0) && (length > 0)) {
    System.out.print(ch3);
    n3--;
    length--;
 }

 while ((n4 > 0) && (length > 0)) {
    System.out.print(ch4);
    n4--;
    length--;
 }

 while ((n5 > 0) && (length > 0)) {
    System.out.print(ch5);
    n5--;
    length--;
}

System.out.println();   
}

This is from another class which uses to give the output:

drawer.drawXmasTree(); System.out.println("\\n"); ;

There are 2 sections of the output.

There are the leaves (top) of the tree, separating the trunk of the tree with ? newline characters.

Leaves:

Starting from the top, you will notice there are size spaces before the first asterisk. The line above that will have size-1 , then the next size-2 , and so on until the number of spaces becomes 0 .

However, this pattern is incomplete. It will only give you the first asterisk of each line. Notice how (starting from the top again) that there are 0 spaces after the first asterisk, (no second asterisk) 1 space after the second asterisk, and so on, until the last line.

Use this pattern and the leaves may be an easy/easier (hopefully) task.

Trunk:

Each asterisk has no spaces after it.

Starting from the top, it prints out size-1 spaces, and then 1 line containing 1 asterisk. Then it prints out size-2 spaces, and immediately afterward, 2 lines of 3 asterisks each.

This pattern continues, each time the number of spaces decreasing by 1 , the number of lines printed with this width increases by 2 (one extra width in either direction), and the number of lines there are increases by 1 .


Please correct me if I have made a mistake. This answer is purely logic of how to print it; this certainly needs to be translated into proper code to function.

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