java 打印等腰三角形,

Python026

java 打印等腰三角形,,第1张

java 打印等腰三角形可以采用如下方式:

public class Mul {

    public static void main(String args[]) {

        for (int i = 1 i <= 6 i++) {

            // 空格分布

            for (int j = 6 - i j > 0 j--) {

                System.out.print(" ")

            }

            // 符号分布

            for (int j = 1 j <= i j++) {

                System.out.print("* ")

            }

            System.out.println()

        }

    }

}

效果如下:

1、实心等边三角形java参考代码如下:

public static void main(String[] args) {  

        int n = 5  

        String c = "0"  

        String x = "*"  

        for (int i = 0 i < n i++) {  

            for (int k = 0 k < n - i - 1 k++) {  

                System.out.print(c)  

            }  

            for (int k = 0 k < i + 1 k++) {  

                System.out.print(x)  

            }  

            for (int k = 0 k < i k++) {  

                System.out.print(x)  

            }  

            /** 

             * 一下注释掉的代码属于多余的代码,本程序只需要分成三块实现 

             */  

//          for (int k = 0 k < n - i - 1 k++) {  

//              System.out.print(c)  

//          }  

            System.out.println()  

        }  

    }

2、空心等边三角形参考代码如下:

public static void main(String[] args) {  

        int n = 6  

        String c = " "  

        String x = "*"  

        for (int i = 0 i < n i++) {  

            for (int j = 0 j < 2 * n j++) {  

                if (j == (n - i) || j == (n + i)) {  

                    System.out.print(x)  

                } else {  

                    System.out.print(c)  

                }  

  

            }  

            System.out.println()  

        }  

        for(int j=0j<2*(n+1)-1j++){  

            System.out.print(x)  

        }  

          

    }

窗口的(Swing)就相对容易一些,只要你找到三个点的坐标就可以通过API画出来

而控制台版就通过打点,打空格来打到画出等腰三角形;

public class DrawTriangleTest extends JFrame {

private Point top

private Point bottom1

private Point bottom2

public static void main(String[] args) {

new DrawTriangleTest()//swing版的三角形

//下面是控制台版的三角形

//-----------------------

        int n = 10  

        String c = "\t"  //分隔符

        String x = "\t*" //打点

        for (int i = 0 i < n i++) {  

            for (int j = 0 j < 2 * n j++) {  

                if (j == (n - i) || j == (n + i)) {  

                    System.out.print(x)  

                } else {  

                    System.out.print(c)  

                }  

  

            }  

            System.out.println()  

        }  

        for(int j=0j<2*(n+1)-1j++){  

            System.out.print(x)  

        }  

        //-----------------------

}

public DrawTriangleTest() {

init()

}

public void init() {

this.setSize(new Dimension(300, 300))

this.setLocationRelativeTo(null)

this.setDefaultCloseOperation(3)

this.setVisible(true)

drawTrinagle(100, 150)

}

public void drawTrinagle(int h, int w) {

int bx = (int) ((this.getWidth() - w) / 2.0)// 低端左边点的x

int ty = (int) ((this.getHeight() - h) / 2.0)//

top = new Point(this.getWidth() / 2, ty)// 定点

bottom1 = new Point(bx, ty + h)// 左下角的点

bottom2 = new Point(bx + w, ty + h)// 右下角的点

this.repaint()

}

@Override

public void paint(Graphics g) {

super.paint(g)

if (top != null && bottom1 != null && bottom2 != null) {

g.drawLine(bottom1.x, bottom1.y, bottom2.x, bottom2.y)// 底下那条线

g.drawLine(top.x, top.y, bottom2.x, bottom2.y)// 左边的腰

g.drawLine(bottom1.x, bottom1.y, top.x, top.y)// 右边的腰

}

}

}