似乎无法正确打印嵌套循环

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

    }

结果只有一列星号,而不是期望的矩形。
请问有人能指出我的错误吗?
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();
    }
}

2
for(int rowCounter=0; rowCounter<height; rowCounter++){
  for(int colCounter=0; colCounter<width; colCounter++){
      System.out.print("*");
  }
  System.out.println();
}

1
你需要添加换行符。
在内部for循环之后,你应该添加以下内容:
System.out.print("\n");
//alternatively just use println which does almost exactly the same

prinln将当前行分隔符写入当前打印流中

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

0

试试这个:

public void draw(){
  for(int rowCounter=0; rowCounter<height; rowCounter++){
    for(int colCounter=0; colCounter<width; colCounter++){
        System.out.print("*");
    }
    System.out.println();
  }
}

0

我认为问题在于你使用了println。Println会在每行末尾打印"\n",因此每次打印"*"时都会打印一个\n并进入下一行。

你可以尝试在draw函数中更改println为print函数,并将

补充完整。
public void draw(){
for(int rowCounter=0; rowCounter<height; rowCounter++){
    for(int colCounter=0; colCounter<width; colCounter++){
        System.out.print("*");
    }
System.out.println("");
}
}

没问题,这种事情发生在我们每个人身上!! - AlvaroAV

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(),并在内部循环后有一个新的行打印


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接