java输出数字梯形_如何在java中旋转,缩进和绘制三角形以打印梯形?

我试图在

Java中绘制一个梯形图.结果很好.现在我正在尝试旋转我的梯形而不在方法rotate()中打印代码.我希望我能做某事.比如在我的testerclass中调用方法rotate()并在调用方法draw()之后,将打印一个旋转的梯形.总而言之,我想要……喜欢

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

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

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

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

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

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

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

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

我的第二个想法是,我想缩进一个单一的梯形.我想在我的其他绘图(缩进)方法中打印梯形.

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

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

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

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

这是我到目前为止我想到的代码

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(…).欢迎任何建议.谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值