2012年3月25日

等腰三角

    public static void main(String[] args) {
        int w = 10;//底邊長
        //case 0 : 無恥數學解
        for(int x = 0 ; x < w/2+1 ; x++){
            for(int y = 0 ; y < w ; y++){
                if(x-y+(w/2) == 0 || x+y-(w/2) == 0 || x == w/2+1-1)
                    System.out.print("* ");
                else 
                    System.out.print("- ");
            }
            //如果 底邊 長度是偶數 則要補一個* 不然會缺一角
            if(x == w/2+1-1 && w%2 == 0)
                System.out.print("* ");
            System.out.println();
        }
        
        System.out.println();
        //case 1 : 苦命解
        for(int x = 0 , cont = w/2 ; x < w/2+1 ; x++ , cont--){
            //印出每行前面的空白
            for(int y = 0 ; y < cont ; y++){
                System.out.print("- ");
            }
            //印出第一個*
            System.out.print("* ");
            //印出空心部分的空白
            if(x != w/2+1-1){
                for(int y = 0 ; y < ((w/2)-cont-1)*2+1 ; y++){
                    System.out.print("- ");
                    //最後印出第二個*
                    if(y == ((w/2)-cont-1)*2+1-1)
                        System.out.print("* ");
                }
            }else if(x == w/2+1-1){//如果到了最後一行則印出整行*
                for(int y = 0 ; y < w-1 ; y++){
                    System.out.print("* ");
                }
                //如果 底邊 長度是偶數 則要補一個* 不然會缺一角
                if(w%2 == 0)
                    System.out.print("* ");
            }
            System.out.println();
        }
    }

}

沒有留言: