Você está na página 1de 2

/* Construct a ChristmasTree class. It contains a main method.

The class takes in user input a


number.
* The number is the number of rows in your Christmas tree including the star.
* For example, if a use inputs 5, the output would be:
* *
* *-*
* *---*
* *-----*
* *-------*
* If the user inputs 2, the output would be:
* *
* *-*
* If the user inputs 10, the output would be:
*1
*
0
*2
*-*
1
*3
*---*
3
*4
*-----* 5
* 5 *-------* 7
* 6 *---------* 9
* 7 *-----------* 11
* 8 *-------------* 13
* 9 *---------------* 15
* 0*-----------------*17
* ...and so forth.
*
*/
import java.util.*;
public class Christmas{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("\nOutput Number of Rows: ");
int s = Integer.parseInt(sc.next());
draw(s);
}
public static void draw(int n){
for(int k = 0; k < n; k++){
for(int j = 1; j <= (n - 1 - k); j++){
System.out.print(" ");
}
System.out.print("*");
for(int i = 1; i <= ((2*k) - 1); i++){
System.out.print("-");
}
if(k != 0){
System.out.print("*");
}
System.out.println();
}
}
}

Você também pode gostar