1. 程式人生 > >JAVA迴圈輸出等腰三角形

JAVA迴圈輸出等腰三角形

  假如給定4行,據計算一行空格加星號為9,即n = 9,程式碼如下:

public class JavaApplication {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int n = 9;
        int a = 1, b;
        for(int i = 0; i < 4; i++)
        {
             b = n - a;
            for(int j = 0; j < b/2; j++)
            {
                System.out.print(" ");
            }
            for(int j = 0; j < a; j++)
            {
                System.out.print("*");
            }
            for(int j = 0; j < b/2; j++)
            {
                System.out.print(" ");
            }
            System.out.println();
            a = a+2;
        }
    }
}