簡體   English   中英

如何在Java中旋轉,縮進和繪制三角形以打印梯形?

[英]How do I rotate, indent and draw a triangle in java to print a trapezium?

我試圖用Java繪制梯形。 效果很好。 現在,我試圖旋轉梯形而不在方法rotate()中打印代碼。 我希望我能做某事。 就像在我的測試器類中調用方法rotate(),並在調用方法draw()之后,將打印旋轉的梯形。 總而言之,我想…… 喜歡

-----*****-----
----*******----
---*********---
--***********--
--***********--
---*********---
----*******----
-----*****-----

我的第二個想法是我想縮進一個梯形。 我想用其他draw(indentation)方法打印梯形。

     --***********--
     ---*********---
     ----*******----
     -----*****-----

這是我到目前為止發現的代碼

public class MyTrapezium { 
    // background character
    private char backgroundChar;

    // number of characters at the bottom line of the trapezium.
    private int bottomWidth;

    // foreground character.
    private char foreground;

    // number of lines used to draw the trapezium
    private int height;

    // the size of the margin in chars.
    private int margin = 5;

    // number of characters at the top line of the trapezium
    private int topWidth;

    // maximal number of characters per line used to draw the trapezium
    private int width = 30;



    // Creates a trapezium without a margin with the given width at the top and bottom.     
    public Trapezium(int topWidth, int bottomWidth) {
        this.topWidth = topWidth;
        this.bottomWidth = bottomWidth;
    }


    public MyTrapezium(int topWidth, int bottomWidth, char foreground, char background, int margin) {
        this.topWidth = topWidth;
        this.bottomWidth = bottomWidth;
        this.foreground = foreground;
        this.background = background;
        this.margin = margin;
    }

    // Creates a trapezium with the given width at the top and bottom and the size of the margin.

    public MyTrapezium(int topWidth, int bottomWidth, int margin) {
        this.topWidth = topWidth;
        this.bottomWidth = bottomWidth;
        this.margin = margin;
    }


    //Methods
    // Draws the trapezium with no indentation.
    public void draw() {
        int tw = topWidth;
        int bw = bottomWidth;
        height = bottomWidth - topWidth;

        while (tw <= bw) {
            for (int i = 0; i < margin; i++) {
                System.out.print(background);
            }
            printChar(background, height / 2);
            printChar(foreground, tw);
            printChar(background, height / 2);
            for (int i = 0; i < margin; i++) {
                System.out.print(background);
            }
            height -= 2;
            tw += 2;
        }
    }

    // Rotates the triangle 180 degrees without printing it
    public void rotate() {

    }

    // Draws the triangle with a given indentation.
    public void draw(int indentation) {
        int i = 0;
        while (i <= indentation) {
            System.out.print(" ");
            i++;
        }
    }

    // Prints a 'run' of a given character.
    private void printChar(char character, int length) {
        for (int i = 0; i < length; i++) {
            System.out.print(character);
        }
    }    

}



public class MyTester {
   public static void main(String[] args) {
        /** Creates and draws a new trapezium with given values.
        * The trapezium is rotated and should be drawn with indentation 15.
        */
        Trapezium t = new Trapezium(5, 11, '*', '-', 2);

        t.draw();
        t.rotate();
        t.draw(15);

    }
}

如您所見,我正在努力反對兩種方法rotate()和draw(...)。 歡迎任何建議。 謝謝。

好的,一種方法是創建一個緩沖區並打印出來,然后...一個緩沖區將是一個String [],其中每個String代表要打印的一行...

private String[] buffer;

private void draw(){ //please rename 'draw' to 'print' this is confusiong else
    for (String line: buffer){
        System.out.println(line);
    }
}

public void create(int topWidth, int bottomWidth, char foreground, char background, int margin){
    //as done in your code
}

public void rotate(){
    String[] newBuffer = new String[buffer.length()];
    for(int i = 0; i < buffer.length(); i ++){
        newBuffer[buffer.length-1-i] = buffer[i];
    }
    buffer = newBuffer;
}

public void draw(int indent){
    String indentString = createIndent(indent);
    for(String line: buffer){
         System.out.println(indentString + line);
    }
}

private String createIndent(int indent){
    String str = "";
    for(int i = 0; i < indent; i ++){
       str = str + " ";
    }
    return str;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM