2011-10-29 85 views
0

我有一个问题,要绘制一个20个字符(大小为'square')的大小限制的正方形ASCII码,这是我的代码,我测试过了,但它工作正常,但是当我把数字超过20它显示一个错误,请帮助,谢谢。使用java绘制ASCII艺术

class Main { 
    public static void printSquare(int size) { 
     int line = 1; 

     while (line <= size) { // For each line of square 
      int width = size; // width of square segment 
      int i = 1; // display square segment 

      while (i <= width && size <= 20) { 
       System.out.print("*"); 
       i = i + 1; 
      } 

      System.out.println(); // Newline 
      line = line + 1; 
     } 
    } 
} 
+0

什么是你的错误? – Marcus

回答

3

的问题是,你永远不会打印*如果大小比20,一种更好的方式将其限制到20会循环之前限制大小。

public static void printSquare(int size) { 
    if(size > 20) { 
     size = 20; 
    } 
    int line = 1; 

,然后编辑

while (i <= width && size <= 20) { 

while (i <= width) { 
+0

谢谢,但我的意思是当尺寸值大于20时将*显示为0,所以我将它们修改为:public static void printSquare(int size) if(size> = 20 && size> = 0){ size = 0; } int line = 1; – user761497

+0

Okey。它没有明确说明,因此我的回答是,下次尝试提供更多的信息:)顺便说一句,您通过在答案左侧勾选大纲来接受答案。 – Marcus