我不明白如何在java中旋转三角形

时间:2016-10-17 22:06:19

标签: java loops

我完全糊涂了。我不是在找人给我答案,我只需要任何帮助。我知道大部分都是错的......很多都被复制和粘贴然后我试图为自己弄清楚,但我不能。谢谢。
这是提示:

  

以下方法打印三个不同形状的三角形。再添加9个名为
的方法   drawTri1R90()drawTri1R180()drawTri1R270()
  drawTri2R90()drawTri2R180()drawTri2R270()
  drawTri3R90()drawTri3R180()drawTri3R270()
  在将原始的90,180和270度向右旋转后,将绘制另外三组三角形。

这是我到目前为止所拥有的,

import java.util.Scanner;

public class JaredLainhart_1 {
    public static void main(String [] args) {
        Scanner in = new Scanner(System.in);
        int size;
        System.out.println("Enter a size:");
        size = in.nextInt();
        Triangle T = new Triangle();
        T.setTriangle(size);
        T.drawTri1();
        T.drawTri2();
        T.drawTri3();
        T.drawTri1R90();
        T.drawTri1R180();
        T.drawTri1R270();
        T.drawTri2R90();
        T.drawTri2R180();
        T.drawTri2R270();
        T.drawTri3R90();
        T.drawTri3R180();
        T.drawTri3R270();
    }
}

import java.util.Scanner;

class Triangle {
    int n;

    void setTriangle(int nn) {
        n = nn;
    }

    void drawTri1() {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print("*");
            System.out.println();
        }
    }

    void drawTri2() {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-i; j++)
                System.out.print(" ");
            for (int j = 1; j <= i; j++)
                System.out.print("*");
            System.out.println();
        }
    }

    void drawTri3() {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-i; j++)
                System.out.print(" ");
            for (int j = 1; j <= 2*i-1; j++)
                System.out.print("*");
            System.out.println();
        }
    }

    void drawTri1R90(){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++)    
                System.out.println(" ");
            for (int j = 1; j <= i; j++)
                System.out.println("*");
            System.out.println();
        }
    }

    void drawTri1R180(){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-1; j++)    
                System.out.println(" ");
            for (int j = 1; j <= 2*i; j++)
                System.out.println("*");
            System.out.println();
        }
    }

    void drawTri1R270(){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-1; j++)    
                System.out.println(" ");
            for (int j = 1; j <= 2*i; j++)
                System.out.println("*");
            System.out.println();
        }
    }

    void drawTri2R90(){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-1; j++)    
                System.out.println(" ");
            for (int j = 1; j <= 2*i; j++)
                System.out.println("*");
            System.out.println();
        }    
    }

    void drawTri2R180(){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-1; j++)    
                System.out.println(" ");
            for (int j = 1; j <= 2*i; j++)
                System.out.println("*");
            System.out.println();
        }    
    }

    void drawTri2R270(){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-1; j++)    
                System.out.println(" ");
            for (int j = 1; j <= 2*i; j++)
                System.out.println("*");
            System.out.println();
        }    
    }

    void drawTri3R90(){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-1; j++)    
                System.out.println(" ");
            for (int j = 1; j <= 2*i; j++)
                System.out.println("*");
            System.out.println();
        }
    }

    void drawTri3R180(){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-1; j++)    
                System.out.println(" ");
            for (int j = 1; j <= 2*i; j++)
                System.out.println("*");
            System.out.println();
        }    
    }

    void drawTri3R270(){
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n-1; j++)    
                System.out.println(" ");
            for (int j = 1; j <= 2*i; j++)
                System.out.println("*");
              System.out.println();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我总是对这些2D问题感到困惑:比如画画加上缩放加上旋转加上着色等等。所以,我所做的是将复杂性降低到一个地方,然后添加它上面的每个功能。

我的意思是:你能画出一个单个三角形吗? (你可以使用corse,因为你完全公开了drawTri1方法)。好吧,然后创建一个方法来做它,用它作为基础。但是不要绘制到System.out,而是绘制到char数组:

public char[][] fillCanvas(int n)
{
    char[][] canvas=new char[n][]
    for (int row=...)
    {
        canvas[row]=new char[n]
        for (int col=...)
        {
            canvas[row][col]=...
        }
    }
    return canvas;
}

然后,编写另一个方法将数组打印到System.out,它将接收画布每个维度的边界作为参数:

private void printCanvas(char[][] canvas, int fromRow, int toRow, int stepRow, int fromCol, int toCol, int stepCol)
{
    for (int row=fromRow;row!=toRow;row+=stepRow)
        for (int col=fromCol;col!=toCol;col+=stepCol)
            System.out.print(...)
        System.out.println()
}

现在你可以编写任意数量的方法来旋转画布,每个方法只需用适当的参数调用printCanvas方法:

public void printCanvasRotated0(char[][] canvas)
{
    int fromRow=0;
    int toRow=canvas.length-1;
    int stepRow=1;
    int fromCol=0;
    int toCol=canvas.length-1;
    int stepCol=1;
    printCanvas(canvas, fromRow, toRow, stepRow, fromCol, toCol, stepCol);
}

这种设计的主要优点是解耦它们之间的程序部分:现在你可以编写几种方法来打印不同种类的形状(三角形,矩形等),但方法<{1}}仍可用于输入的任何画布,因此您可以将它们联合起来。

注意:请记住,在Java中,数组位置始终从0到n-1。