使用JAVA输出“*”组成的不同三角形

使用 * 打印一个三角形,如图所示:

*
**
***
****
--------------------------------------------------------------------
public class Excerise {
    public static void main(String[] args){
        for(int i=1;i<5;i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
****
***
**
*
-----------------------------------------------------------------------
public class Excerise {
    public static void main(String[] args){
        for(int i=1;i<5;i++) {
            for (int j = 5; j >i; j--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
*
**
***
****
***
**
*
--------------------------------------------------------
public class Excerise {
    public static void main(String[] args){
        for(int i=1;i<=7;i++) {
            if(i<4) {
                for (int j = 1; j <= i; j++) {
                    System.out.print("*");
                }
                System.out.println();
            }else{
                for (int j = 7; j >= i; j--) {
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    }
}
*
 *
  *
   *
  *
 *
*
-----------------------------------------------------------
public class Excerise {
    public static void main(String[] args){
        for(int i=1;i<=7;i++) {
            if(i<4) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(" ");
                }
                System.out.println("*");
            }else{
                for (int j = 7; j >= i; j--) {
                    System.out.print(" ");
                }
                System.out.println("*");
            }
        }
    }
}
     *
    **
   ***
  ****
 *****
-----------------------------------------
public class Excerise {
    public static void main(String[] args){
        for(int i=1;i<=5;i++){
            //循环输出左侧空白区域
            for(int j=5; i<=j; j--)
                System.out.print(" ");
            //循环输出右侧*号区域
            for(int j=1; j<=i; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}
     *
    ***
   *****
  *******
 *********
------------------------------------------------------------
public class Excerise {
    public static void main(String[] args){
        for(int i=1;i<=5;i++){
            //循环输出左侧的空白三角形区域
            for(int j=5; i<=j; 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();
        }
    }
}

上图的代码这里有详细的解释:http://www.runoob.com/w3cnote/java-print-the-triangle.html

猜你喜欢

转载自blog.csdn.net/AI_drag0n/article/details/84502594