2013-10-16 85 views
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(); 
    } 

    } 

結果是星號而不是希望的矩形的一個長列。

有人可以請指出我做錯了什麼。

回答

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

試試這個:

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

您需要添加換行符。

後你內心的循環,你應該添加以下內容:

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

prinln writes the current line.separator to the current Print Stream

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

我認爲問題在於您使用的是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(""); 
} 
} 
+0

謝謝你們,我現在覺得自己很蠢 –

+0

沒有問題,這事發生在我們所有的人! – 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"); 
}