幅と高さの 2 つの次元が指定されたアスタリスクの四角形を描画する単純なプログラムを作成しようとしています。ここにコードがあります
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();
}
}
結果は、期待される Rectangle ではなく、アスタリスクの 1 つの長い列になります。
誰かが私が間違っていることを指摘してください。