zoukankan      html  css  js  c++  java
  • java--用 * 打印出各种图形(新手请进)

    ------------------------------------

    代码:

    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();
            }

        }

    }

  • 相关阅读:
    oracle修改字符编码
    oracle修改约束列
    oracle非空约束
    Linux修改字符集
    修改oracle字符集合
    word问题禁止宏
    增加修改表列
    oracle增加sequence
    增加 修改oracle约束条件
    oracle用户 密码永不过期
  • 原文地址:https://www.cnblogs.com/hsjie/p/java.html
Copyright © 2011-2022 走看看