Pages

2013-05-02

[Java]Show a X'mas tree


Please show a X'mas tree in the window using Java




code :
import java.util.Scanner;

public class XmasTree {

public static void main(String[] args) {
  
  System.out.println("Print a X'mas Tree of height H\nH=");
  Scanner scanner = new Scanner(System.in);
  int a = scanner.nextInt();
  
  for (int x=0;x<a+1;x++){
  
   for (int i=1;i<a+1-x;i++){
    System.out.print(" ");
    }
   
   for (int j=x*2-1;j>0;j--){
    System.out.print("*");
   }
   
   System.out.println();
   
  }
  
  for (int k=0;k<3;k++){
   for (int l=0;l<a-3;l++){
   System.out.print(" ");
   }

   System.out.println("* * *");

  }
    
 }

}
output :

No comments:

Post a Comment