2012-11-20 47 views
2

我想颠倒这段代码,以便我可以打印带有边框的菱形图案。开始反转嵌套循环/一系列循环的最佳方法是什么?我试着对代码进行修改,但是一切都混乱无序。有什么建议?颠倒嵌套循环

另外,有没有一种方法可以在顶部星星的每一边制作偶数个。我已经能够使它发挥作用的唯一途径打印偶数每侧由一个量...

这里是应该打印到控制台:http://i.imgur.com/h55r2.jpg

这里是我的代码:

public class SixTester { 

    public static void main(String[] args) 
    { 
      int i,j,k; 
      int numOfRows = 8; // Made this a variable, so that the program can make any size diamond (try playing around with different values eg. 2 or 16) 

      // Step 1. The first Dash 
      for(i=0;i<numOfRows*2 +2;i++) 
        System.out.print(" "); // Number of spaces is double the number of rows in your 'Half Pyramid' 
      System.out.println("-"); 

      // Step 2. The First half diamond 
      for (j=0; j<numOfRows ; j++) 
      { 
        for(k=numOfRows*2; k>1+j*2; k--) 
          System.out.print(" "); 

        System.out.print("_/"); 
        for (i=0; i< 2+j*4; i++) 
        { 
          // Prepare the Star rectangle, Note that it starts half the way (rows/2) 
          if(j >= numOfRows/2 && (i>j*2- numOfRows/2 && i<j*2+ numOfRows/2)) { 
            System.out.print("*");         
          } 
          else 
            System.out.print("."); 
        } 
        System.out.println("\\_"); 
      } 
      // Next Step - Make the bottom pyramid...but how to reverse? 
    } 
} 
+1

+1用于拍摄ascii艺术照片 – Adam

+0

我宁愿为每个*重*动作都采用一种方法。通过沉重的行动,我的意思是每个包含逻辑代码的“for-loop”。一旦你设计了这些方法,就可以更容易地反转当前的代码。否则,您应该复制并修改当前代码以具有反向打印行为。 –

回答

2

这不是最优雅的方式,但它的工作原理。在您的代码所在的位置插入这些行,“但如何反转?”我已经打上了与评论

 // COUNT BACKWARDS NOW. YOU WANT LARGEST ROW FIRST, OTHERWISE IT'S OK EXCEPT... 
     for (j=numOfRows-1; j>=0 ; j--) 
     { 
       for(k=numOfRows*2; k>1+j*2; k--) 
         System.out.print(" "); 

       System.out.print("\\_"); // BORDERS ARE BACKWARDS. PUT BORDER ON OTHER SIDE 
       for (i=0; i< 2+j*4; i++) 
       { 
         if(j >= numOfRows/2 && (i>j*2- numOfRows/2 && i<j*2+ numOfRows/2)) { 
           System.out.print("*");         
         } 
         else 
           System.out.print("."); 
       } 
       System.out.println("_/"); // PUT BORDER ON OTHER SIDE 
     } 

     for(i=0;i<numOfRows*2 +2;i++) 
       System.out.print(" "); 
     System.out.println("-"); 
+0

我正在寻找一些关于自己做这些事的技巧,但我觉得它开始时相当含糊。感谢你的回答!我看到的唯一一个问题是:我希望中心的星形矩形为8x8,我需要改变底部金字塔中的。的数量以匹配[this](http://i.imgur。)。 COM/h55r2.jpg)。非常感谢你,+1! – dustdustdust

+2

好吧,我很高兴我的解决方案不是完美的,希望这给你一些正确的方向提示。记住编程是关于从小工作中构建大型工作,如果你正确地分析小工作,修复大错误应该容易得多。 – durron597

+0

+1看到我的解决方案超过工程:) – Adam

0

如果你写了这样一个建设者的变化对您的代码......

public static interface Reflection { 
    String reflect(String str); 
} 

public static class Builder { 
    private List<String> lines = new ArrayList<String>(); 
    private StringBuilder builder = new StringBuilder(); 
    public void newLine() { 
     lines.add(builder.toString()); 
     builder = new StringBuilder(); 
    } 
    public void repeat(String section, int count) { 
     for (int i = 0; i < count; i++) { 
      builder.append(section); 
     } 
    } 
    public void padLeft(String section, int count) { 
     while (builder.length() < count) { 
      builder.append(section, 0, section.length()); 
     } 
    } 
    public void reflectX(Reflection reflection) { 
     List<String> reflected = new ArrayList<String>(); 
     for (String line : lines) { 
      StringBuilder tmp = new StringBuilder(); 
      tmp.append(reflection.reflect(line)); 
      tmp.reverse(); 
      tmp.append(line); 
      reflected.add(tmp.toString()); 
     } 
     lines = reflected; 
    } 
    public void reflectY(Reflection reflect) { 
     List<String> reflection = new ArrayList<String>(); 
     for (String line : lines) { 
      reflection.add(reflect.reflect(line)); 
     } 
     Collections.reverse(reflection); 
     lines.addAll(reflection); 
    } 
    public String build() { 
     StringBuilder tmp = new StringBuilder(); 
     for (String line : lines) { 
      tmp.append(line); 
      tmp.append('\n'); 
     } 
     return tmp.toString(); 
    } 
    public void write(String string) { 
     builder.append(string); 
    } 
} 

那么你的代码的代码可以简化为这一点,但是这也只是在工程:

int nRows = 8; 
int pad = 20; 
Builder builder = new Builder(); 

builder.write("_"); 
builder.padLeft(" ", pad); 
builder.newLine(); 

for (int i = 0; i < nRows; i++) { 
    int dots = i * 2 + 1; 
    int stars = i >= 4 ? 4 : 0; 
    builder.repeat("*", stars); 
    builder.repeat(".", dots - stars); 
    builder.write("\\"); 
    if (i < nRows - 1) { 
     builder.write("_"); 
    } 
    builder.padLeft(" ", pad); 
    builder.newLine(); 
} 
builder.reflectX(new Reflection() { 
    @Override 
    public String reflect(String str) { 
     return str.replace('\\', '/'); 
    } 
}); 
builder.reflectY(new Reflection() { 
    @Override 
    public String reflect(String str) { 
     return str.replace("\\", "%x").replace("/", "\\").replace("%x", "/"). 
       replace("_\\", "%x").replace("/_", "_/").replace("%x", "\\_"); 
    } 
}); 
System.out.println(builder.build()); 

结果

   __     
      _/..\_     
      _/......\_    
     _/..........\_    
     _/..............\_   
    _/.....********.....\_   
    _/.......********.......\_  
_/.........********.........\_  
/...........********...........\  
\...........********.........../  
\_.........********........._/  
    \_.......********......._/  
    \_.....********....._/   
     \_.............._/   
     \_.........._/    
      \_......_/    
      \_.._/     
       __