我正在尝试编写一个简单的程序,根据给定的宽度和高度绘制一个星号矩形。以下是代码:
结果只有一列星号,而不是期望的矩形。
请问有人能指出我的错误吗?
public class Rectangle {
private int width, height;
public Rectangle(){
System.out.println("A rectangle created: width = 10 and height = 25");
width = 10;
height = 25;
}
public Rectangle(int w, int h){
if (w > 0 && w < 30 && h > 0 && h < 30){
width = w;
height = h;
System.out.println("Rectangle Created: Height = "+h+" and width = "+w);
}else{
System.out.println("Invalid values for rectangle, Values ,ust be positive and less than 30");
}
}
public int getArea(){
return width * height;
}
public void draw(){
for(int rowCounter=0; rowCounter<height; rowCounter++){
for(int colCounter=0; colCounter<width; colCounter++){
System.out.println("*");
}
}
}
}
以下是我的矩形测试代码
public class TestRectangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle r1 = new Rectangle();
r1.draw();
Rectangle r2 = new Rectangle(15,5);
System.out.println("Area of rectangle r2: " +r2.getArea());
r2.draw();
}
}
结果只有一列星号,而不是期望的矩形。
请问有人能指出我的错误吗?