DALLASCAO.COM

Site of Cao Shouguang, English to Chinese translator

Learn Java: print an asterisk tower

/**
Print a asterisk tower.
Learn Java practice
OUTPUT
         *         
        ***        
       *****       
      *******      
     *********     
    ***********    
   *************   
  ***************  
 ***************** 
*******************

@author Yifan Chao
*/
public class Ast {
/**
app entry point
*/
    public static void main(String[] args) {
        for (int counter1 = 1; counter1 <= 10; counter1++) {
            int asterik_count = counter1 * 2 -1;
            int space_count = 10 - counter1;
            char output_char = ' ';            
            for (int counter2 = 1; counter2 <= 19; counter2++) {                
                if ((counter2 <= space_count + asterik_count ) && (counter2 > space_count)) {
                     output_char = '*';
                } else {
                	output_char = ' ';                	
                }                
                System.out.print(output_char);                
            }
            System.out.println();            
        }
    }
}

Learnt:
1. Double quote for String, and Single quote for Char.
2. Eclipse is great! IDE is a must for beginners.

Leave a Comment