等腰三角形的打印

来源:互联网 发布:衣服品牌查询软件 编辑:程序博客网 时间:2024/05/16 05:24

打印出一个等腰三角形。
思路很容易:双重for循环处理。
难点在于如何控制等腰,让图形像个金字塔,可以想象一个矩形挖成等腰三角形


package com.math.forth;/*** * 打印出一个等腰三角形。 思路很容易:双重for循环处理。  * 难点在于如何控制等腰,让图形像个金字塔 *  * @author wql * */public class Math12 {    public static void main(String[] args) {        method();    }    public static void method() {        for (int i = 1; i <= 9; i++) {            for (int j = 9; j >= 1; j--) {                if (j <= i) {                    System.out.print("* ");                } else                    System.out.print(" ");            }            System.out.println();// 换行        }    }    public static void method2() {        for (int i = 1; i <= 9; i++) {            for (int j = 1; j <= 9; j++) {                if (i < 10 - j) {                    System.out.print(" ");                } else                    System.out.print("* ");            }            System.out.println();// 换行        }    }}

这里写图片描述

原创粉丝点击