我正在尝试编写一个简单的程序,该程序在给定两个维度(宽度和高度)的情况下绘制一个星号矩形。这是代码
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();
}
}
结果是一长列星号,而不是希望的矩形。
有人可以指出我做错了什么。