ゞ沉默是金ゞ

鱼离不开水,但是没有说不离开哪滴水.
posts - 98,comments - 104,trackbacks - 0
先看效果:


代码:

public class PrintTriangle {
    
    
public static void main(String[] args) {
        System.out.println(
"======左边正三角形======");
        printTopLeft(
5);
        System.out.println(
"======左边倒三角形======");
        printDownLeft(
5);
        System.out.println(
"======打印右正三角形======");
        printTopRight(
5);
        System.out.println(
"======打印右倒三角形======");
        printDownRight(
5);
        System.out.println(
"======打印正中三角形======");
        printTopCenter(
5);
        System.out.println(
"======打印倒中三角形======");
        printDownCenter(
5);
        System.out.println(
"======打印中正空三角形======");
        printTopMidBlank(
5);
        System.out.println(
"======打印中倒空三角形======");
        printDownMidBlank(
5);
        System.out.println(
"======打印正人字状======");
        printTopRen(
5);

    }

    
/*
     * 打印正人字状
     
*/

    
public static void printTopRen(int n){
        
for(int i = 1; i <= n; i ++){
            
for(int j = i; j < n; j ++){
                System.out.print(
"   ");
            }

            
//左半部分
            for(int j = 1; j <= i; j++){
                
if(j == 1)
                    System.out.print(
" * ");
                
else
                    System.out.print(
"   ");
            }

            
//右半部分
            for(int j = 1; j < i; j++){
                
if(j == i - 1)
                    System.out.print(
" * ");
                
else 
                    System.out.print(
"   ");
            }

            System.out.println();
        }

    }

    
/*
     * 打印倒空三角形
     
*/

    
public static void printDownMidBlank(int n){
        
for(int i = 1; i <= n; i ++){
            
for(int j = 1; j < i; j ++){
                System.out.print(
"   ");
            }

            
for(int j = i; j <= n; j ++){
                
if(i == 1 || j == i|| i == n)
                    System.out.print(
" * ");
                
else
                    System.out.print(
"   ");
            }

            
for(int j = i; j < n; j ++){
                
if(i == 1 || j == n - 1)
                    System.out.print(
" * ");
                
else
                    System.out.print(
"   ");
            }

            System.out.println();
        }

    }

    
/*
     * 打印中正空三角形
     
*/

    
public static void printTopMidBlank(int n){
        
for(int i = 1; i <= n; i ++){
            
for(int j = i; j < n; j ++){
                System.out.print(
"   ");
            }

            
//左半部分
            for(int j = 1; j <= i; j++){
                
if(j == 1 || i == n)
                    System.out.print(
" * ");
                
else
                    System.out.print(
"   ");
            }

            
//右半部分
            for(int j = 1; j < i; j++){
                
if(j == i - 1 || i == n)
                    System.out.print(
" * ");
                
else 
                    System.out.print(
"   ");
            }

            System.out.println();
        }

    }

    
/*
     * 打印倒中三角形
     
*/

    
public static void printDownCenter(int n){
        
for(int i = 1; i <= n; i ++){
            
for(int j = 1; j < i; j ++){
                System.out.print(
"   ");
            }

            
for(int j = i; j <= n; j ++){
                System.out.print(
" * ");
            }

            
for(int j = i; j < n; j ++){
                System.out.print(
" * ");
            }

            System.out.println();
        }

    }

    
/*
     * 打印正中三角形
     
*/

    
public static void printTopCenter(int n){
        
for(int i = 1; i <= n; i ++){
            
for(int j = i; j < n; j ++){
                System.out.print(
"   ");
            }

            
//左半部分
            for(int j = 1; j <= i; j++){
                System.out.print(
" * ");
            }

            
//右半部分
            for(int j = 1; j < i; j++){
                System.out.print(
" * ");
            }

            System.out.println();
        }

    }

    
/*
     * 打印右倒三角形
     
*/

    
public static void printDownRight(int n){
        
for(int i = n; i >= 1; i --){
            
for(int j = n; j > i; j --){
                System.out.print(
"   ");
            }

            
for(int j = i; j >= 1; j --){
                System.out.print(
" * ");
            }

            System.out.println();
        }

    }

    
/*
     * 打印右正三角形
     
*/

    
public static void printTopRight(int n){
        
for(int i = 1; i <= n; i ++){
            
for(int j = n; j > i; j --){
                System.out.print(
"   ");
            }

            
for(int j = i; j >= 1; j--){
                System.out.print(
" * ");
            }

            System.out.println();
        }

    }

    
/*
     * 打印左正三角型
     
*/

    
public static void printTopLeft(int n){
        
for(int i = 1; i <= n; i ++){
            
for(int j = 1; j <= i; j ++){
                System.out.print(
" * ");
            }

            System.out.println();
        }

    }

    
/*
     * 打印左倒三角形
     
*/

    
public static void printDownLeft(int n){
        
for(int i = 1; i <= n; i ++){
            
for(int j = n; j >= i; j --){
                System.out.print(
" * ");
            }

            System.out.println();
        }

    }

}

希望大家多提意见。
posted on 2009-06-02 14:43 ゞ沉默是金ゞ 阅读(1394) 评论(2)  编辑  收藏 所属分类: Java SE

FeedBack:
# re: 用 * 打印出各种图形
2009-06-02 15:03 | FEJAY
学习了   回复  更多评论
  
# re: 用 * 打印出各种图形
2009-06-03 17:14 | thebye85
看得我都是星星  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: