JAVA循环输出等腰三角形

来源:互联网 发布:win7时间校准软件 编辑:程序博客网 时间:2024/06/05 09:00

  假如给定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;        }    }}