3

我正在尝试编写一个简单的程序,该程序在给定两个维度(宽度和高度)的情况下绘制一个星号矩形。这是代码

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();
    }

    }

结果是一长列星号,而不是希望的矩形。

有人可以指出我做错了什么。

4

7 回答 7

3

println打印参数,然后是换行符。

您需要print在内部循环内部使用,并且println为每个外部循环使用一次。

public void draw(){
    for(int rowCounter=0; rowCounter<height; rowCounter++){
        for(int colCounter=0; colCounter<width; colCounter++){
            System.out.print("*");
        }
        System.out.println();
    }
}
于 2013-10-16T09:37:48.963 回答
2
for(int rowCounter=0; rowCounter<height; rowCounter++){
  for(int colCounter=0; colCounter<width; colCounter++){
      System.out.print("*");
  }
  System.out.println();
}
于 2013-10-16T09:38:21.150 回答
1

您需要添加换行符。

在您的内部 for 循环之后,您应该添加以下内容:

System.out.print("\n");
//alternatively just use println which does almost exactly the same

prinln 将当前 line.separator 写入当前打印流

println("foo"); == print("foo"); println(); == print("foo"); print(line.separator);

而 line.separator 通常是,但并不总是是 "\n"

这使您的代码看起来像这样:

for(int rowCounter=0; rowCounter<height; rowCounter++){
    for(int colCounter=0; colCounter<width; colCounter++){
        System.out.print("*");
    }
    System.out.print("\n");
    }
}
于 2013-10-16T09:40:30.657 回答
0

试试这个它会为你工作

在您的内部循环之后,您应该添加以下内容:

System.out.print("\n");

特定于您的代码:

for(int rowCounter=0; rowCounter<height; rowCounter++){
   for(int colCounter=0; colCounter<width; colCounter++){
      System.out.print("*");
   }
   System.out.print("\n");
}
于 2013-10-16T09:44:33.230 回答
0

尝试这个:

public void draw(){
  for(int rowCounter=0; rowCounter<height; rowCounter++){
    for(int colCounter=0; colCounter<width; colCounter++){
        System.out.print("*");
    }
    System.out.println();
  }
}
于 2013-10-16T09:39:27.960 回答
0

我认为问题在于您使用的是 println。Println 在每一行的末尾打印“\n”,因此每次打印“*”时都会打印一个\n,然后转到下一行。

你能在绘图功能中试试这个吗?只需将 println 更改为打印功能,然后制作

public void draw(){
for(int rowCounter=0; rowCounter<height; rowCounter++){
    for(int colCounter=0; colCounter<width; colCounter++){
        System.out.print("*");
    }
System.out.println("");
}
}
于 2013-10-16T09:41:49.393 回答
0
public void draw(){
for(int rowCounter=0; rowCounter<height; rowCounter++){
    System.out.println();
    for(int colCounter=0; colCounter<width; colCounter++){
        System.out.print("*");
    }
    System.out.println();
}
}

println(), 将在打印到控制台后创建一个新行,所以在你的内部循环中你不应该有 println,而是使用print()并且在内部循环之后有一个新行打印

于 2013-10-16T09:41:49.790 回答