Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
我试图弄清楚如何使用嵌套的for循环在下面产生这种模式:

java - 使用嵌套的for循环绘制等边三角形?-LMLPHP

到目前为止,我有

public class Triangle {

public static final int SIZE = 600;
public static final int INITIAL_X = 300;
public static final int INITIAL_Y = 50;
public static final int SIDE = 100;

/**
 *
 *
 * @param args command line arguments
 */
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    DrawingPanel panel = new DrawingPanel(SIZE, SIZE);
    panel.setBackground(Color.WHITE);
    System.out.print("Enter number of rows (1-5): " );
    int row = console.nextInt();
    if(row < 1) {
        row = 1;
    } else if(row > 5) {
        row = 5;
    }
    System.out.print("Specify red value (0-255): ");
    int red = console.nextInt();
    if(red < 0) {
        red = 0;
    } else if(red > 255) {
        red = 255;
    System.out.print("Specify green value (0-255): ");
    int green = console.nextInt();
    if(green < 0) {
        green = 0;
    } else if(green > 255) {
        green = 255;
    }
    System.out.print("Specify blue value (0-255): ");
    int blue = console.nextInt();
    if(blue < 0) {
        blue = 0;
    } else if(blue > 255) {
        blue = 255;
    }
    Graphics g = panel.getGraphics(); //Initializes graphics panel.
    g.setColor(new Color(red, green, blue));

    for (int i = 1; i <= row; i++) {
        for (int x = 1; x <= row; x++) {
            int lx = sx - SIDE/2;
            int rx = sx + SIDE/2;
            int ly = (int) (sy + SIDE*Math.sqrt(3)/2);
            int ry = ly;


    System.out.println("\n*CLOSE the Drawing Panel to exit the program*");
}

/**
 * Draws an equilateral triangle with topmost point at (x, y) with the
 * given side length and color.
 *
 * @param g
 * @param color
 * @param x
 * @param y
 * @param sideLength
 */

public static void drawTriangle(Graphics g, int sx, int sy, int rows) {
    g.drawLine(sx, sy, lx, ly);
    g.drawLine(sx, sy, rx, ry);
    g.drawLine(lx, ly, rx, ry);
}


这段代码肯定还没有完成。我只想使用java.awt。*和java.util。*包来解决此问题。我的麻烦主要是在main方法中创建一个嵌套的for循环,但我相信我对这项任务的扫描程序部分有所了解。我使用了下面的等边三角形公式,这些公式是根据毕达哥拉斯定理得出的。每行应具有许多三角形(例如,第5行应具有5个三角形)。三角形是连续的,每个三角形的左下/右下角形成下面一行中三角形的最高点。第一行中三角形最高点的(x,y)坐标必须为(300,50)。请让我知道是否有人可以提供帮助。

java - 使用嵌套的for循环绘制等边三角形?-LMLPHP

最佳答案

递归方法听起来比迭代容易,因此看起来像这样:

private void drawTree(double x, double y, int depth, int maxDepth, Graphics graphics, double sideLength) {
    if (depth >= maxDepth) {
        return;
    }

    double leftX = x - sideLength / 2;
    double leftY = y + Math.sqrt(sideLength * 3) / 2;
    double rightX = x + sideLength / 2;
    double rightY = leftY;

    //draw line from (x,y) -> (leftX, leftY)
    graphics.drawLine(x, y, leftX, leftY);
    //draw line from (x,y) -> (rightX, rightY)
    graphics.drawLine(x, y, rightX, rightY);
    //draw line from (leftX, leftY) -> (rightX, rightY)
    graphics.drawLine(leftX, leftX, rightX, rightY);

    drawTree(leftX, leftY, depth + 1, maxDepth, graphics, sideLength);
    drawTree(rightX, rightY, depth + 1, maxDepth, graphics, sideLength);
}

10-08 09:22