.定义一个类 Draw , 在 类 中 提供 3 个 方法:输出直角三角形(drawTrian()) 、 输出矩形(drawRec()) 及平行四边形(drawPra()) 。 通过方法可以输出由“*” 组成的一个图形。 同时在类中包含两个属性:星号的个数(count) 、行数(lines) 。最后在测试类中进行调用。
代码:
public class ZuoYeDemo05 {
public static void main(String[] args) {
Draw d=new Draw(5,4);
d.drawPra(d.getLines());
d.drawRec(d.getLines(), d.getCount());
d.drawTrian(d.getLines());
}
}
class Draw{
private int count;
private int lines;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getLines() {
return lines;
}
public void setLines(int lines) {
this.lines = lines;
}
public Draw(int count, int lines) {
this.count = count;
this.lines = lines;
}
public void drawTrian(int lines){
for (int i=0;i<lines;i++){
for (int j=0;j<i;j++){
System.out.print("*");}
System.out.println();
}
System.out.println("===============================");
}
public void drawRec(int lines,int count){
for (int i=0;i<lines;i++){
for (int j=0;j<(count);j++){
System.out.print("*");
}
System.out.println();
}
System.out.println("==============================");
}
public void drawPra(int lines){
for (int i = 0; i < lines; i++) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" "); // 输出左上角位置的空白
}
for (int j = 0; j < lines; j++) {
System.out.print("*"); // 输出菱形上左半部边缘
}
System.out.println("");
}
System.out.println("==================================");
}
}