2014-10-08 194 views
3

所以我知道有類似的問題,但他們都沒有看起來這個「複雜」。 這是程序的預期輸出。嵌套for循環藝術

 /**\ 
    //**\\ 
    ///**\\\ 
    ////**\\\\ 
/////**\\\\\ 
+=*=*=*=*=*=*+ 
|../\..../\..| 
|./\/\../\/\.| 
|/\/\/\/\/\/\| 
|\/\/\/\/\/\/| 
|.\/\/..\/\/.| 
|..\/....\/..| 
+=*=*=*=*=*=*+ 
|\/\/\/\/\/\/| 
|.\/\/..\/\/.| 
|..\/....\/..| 
|../\..../\..| 
|./\/\../\/\.| 
|/\/\/\/\/\/\| 
+=*=*=*=*=*=*+ 
    /**\ 
    //**\\ 
    ///**\\\ 
////**\\\\ 
/////**\\\\\ 

這是我的for循環構造體

divider(); 
    for (int fb=0;fb<=2;fb++) { 
     System.out.print("|"); 
     System.out.print(fSlash+bSlash); 
      for (int i=0;i<=fb*2;i++) { 
       System.out.print(fSlash+bSlash); 
      } 
    } 
    for (int fb=2;fb>=0;fb--) { 
     System.out.print("|"); 
     System.out.print(bSlash+fSlash); 
      for (int i=fb;i<=fb*3;i++) { 
       System.out.print(bSlash+fSlash); 
     } 
    } 

正如你可以看到我缺少的下降週期。

+0

所以包括他們?你甚至沒有試圖打印它們。 – 2014-10-08 01:02:37

+0

你的問題是你正在考慮用你想要製作頭部和尾部的方式來創建身體。但是兩者之間有根本的區別。頭部和尾部(你得到的權利)向外擴展。出於顯而易見的原因,您不能對身體使用相同類型的算法或方法。試着想一些新的東西,而不是重複使用已經提出的東西。 – asteri 2014-10-08 01:03:28

+0

@asteri我想我需要將/ \嵌套到產生.sl – EwokHugz 2014-10-08 01:13:54

回答

2
public static void topCenter(String fSlash, String bSlash) { 
    divider(); 
    for(int fb = 0; fb <= 2; fb++) { 
     System.out.print("|"); 
     for(int repeat = 0; repeat < 2; repeat++) { 
      printDots(2 - fb); 
      for(int i = 0; i <= fb; i++) { 
       System.out.print(fSlash + bSlash); 
      } 
      printDots(2 - fb); 
     } 
     System.out.println("|"); 
    } 
    for(int fb = 2; fb >= 0; fb--) { 
     System.out.print("|"); 
     for(int repeat = 0; repeat < 2; repeat++) { 
      printDots(2 - fb); 
      for(int i = fb; i <= fb * 2; i++) { 
       System.out.print(bSlash + fSlash); 
      } 
      printDots(2 - fb); 
     } 
     System.out.println("|"); 
    } 
    //bottomCenter(fSlash, bSlash); 
} 

public static void printDots(final int count) { 
    for(int i = 0; i < count; i++) { 
     System.out.print("."); 
    } 
} 
+0

應該是printDots(fb);但除此之外,它的作品。謝謝。 – EwokHugz 2014-10-08 01:35:51

+0

不,它不應該。在第一次迭代中'fb'爲零,但你需要2個點。所以你需要'2-fb'。 – Jason 2014-10-08 01:36:20

+0

你說得對,我的printDot()方法設置不同。謝謝 – EwokHugz 2014-10-08 01:46:28

2

雖然這個問題已經得到解答,當我在學校看到它,並且在今天到家之後,我開始自己實現它,這讓我感到很癢。以爲我會在這裏分享它,因爲它可能對你有用。如果您想知道,我將所有內容都附加在StringBuilder之上,以避免其他進程正在使用的System.out printstream的潛在錯誤,並將藝術浪費掉。因此只需一次打印所有內容。

/** 
* Created by Thomas on 08/10/2014 at 4:53 PM. 
* 
* @author Thomas 
* @since X.X.X 
*/ 
public class CharacterArt { 

/** 
* Makes a triangle with astrix's along the center, and slashes on the sides. 
* 
* @param height   The height of the triangle, total width is determined off this. 
* @param middleWidth  The width of the characters in the middle of the triangle. 
* @param sideBufferExtra How much buffering to put on each side of the triangle, this is used 
*      (in the OP's example) for the extra spacing required to be flush with 
*      the rest of the piece. 
* @return The triangle with the passed parameters. 
*/ 
public static String makeACenteredTriangle(int height, int middleWidth, int sideBufferExtra) { 
    StringBuilder builder = new StringBuilder(); 
    for (int row = 1; row <= height; row++) { 

     //Left buffer 
     for (int b = 1; b <= height - row + sideBufferExtra; b++) 
      builder.append(' '); 

     //Left slashes 
     for (int slash = 1; slash <= row; slash++) 
      builder.append('/'); 

     //Middle column 
     for (int mid = 1; mid <= middleWidth; mid++) 
      builder.append('*'); 

     //Right slashes 
     for (int slash = 1; slash <= row; slash++) 
      builder.append('\\'); 

     //Right buffer 
     for (int b = 1; b <= height - row + sideBufferExtra; b++) 
      builder.append(' '); 

     builder.append('\n'); 
    } 
    return builder.toString(); 
} 

/** 
* Creates a strip of a diamond ascii art - piece. 
* 
* @param sideLength  Length of each side of the diamonds. 
* @param numberDiamonds Number of diamonds to append to the line. 
* @param rowNumber  Which row of the diamond to be generated. Starting at row index 1 and 
*      going up to sideLength * 2 
* @return The row of the diamond 
*/ 
public static String makeADiamondsStrip(int sideLength, int numberDiamonds, int rowNumber) { 
    StringBuilder builder = new StringBuilder(); 

    //For the number of diamonds 
    for (int number = 1; number <= numberDiamonds; number++) { 

     //Left buffering 
     if (rowNumber <= sideLength) 
      for (int b = 1; b <= sideLength - rowNumber; b++) builder.append('.'); 
     else 
      for (int b = 1; b <= rowNumber - sideLength - 1; b++) builder.append('.'); 

     //Slashes 
     if (rowNumber <= sideLength) 
      for (int s = 1; s <= rowNumber; s++) 
       builder.append("/\\"); 
     else 
      for (int s = 1; s <= rowNumber - 2 * (rowNumber - sideLength) + 1; s++) 
       builder.append("\\/"); 

     //Right buffering 
     if (rowNumber <= sideLength) 
      for (int b = 1; b <= sideLength - rowNumber; b++) builder.append('.'); 
     else 
      for (int b = 1; b <= rowNumber - sideLength - 1; b++) builder.append('.'); 
    } 
    return builder.toString(); 
} 

/** 
* Not working the best, though gets the basic job done. 
* 
* @param totalWidth Total width of the divider, must be an even number as of now. 
* @param middleWidth Width of the middle characters in the divider, as of now must be an even 
*     number. 
* @param sideWidth Width of the '+' characters on each side of the divider. 
* @return The divider. 
*/ 
public static String makeADivider(int totalWidth, int middleWidth, int sideWidth) { 
    StringBuilder builder = new StringBuilder(); 

    int remainingEachSide = (totalWidth - middleWidth - 2 * sideWidth)/2; 

    for (int i = 0; i < sideWidth; i++) builder.append('+'); 

    //Left 
    for (int left = 1; left <= remainingEachSide; left++) 
     builder.append(left % 2 == 1 ? '=' : '*'); 
    //Middle 
    for (int middle = 1; middle <= middleWidth; middle++) builder.append('*'); 

    //Right 
    for (int right = 1; right <= remainingEachSide; right++) 
     builder.append(right % 2 == 1 ? '=' : '*'); 

    for (int i = 0; i < sideWidth; i++) builder.append('+'); 


    return builder.toString(); 
} 

public static void main(String[] args) { 

    /* Initialise our StringBuilder. */ 
    StringBuilder builder = new StringBuilder(); 

    /* Append the first triangle to the top, with a height of 5, middle column width of 2 and a side-padding of 1. */ 
    builder.append(makeACenteredTriangle(5, 2, 1)); 

    /* Append the first divider, with a width of 14 having a total middle column width of 2, and 1 '+' at each end. */ 
    builder.append(makeADivider(14, 2, 1)).append('\n'); 

    /* 
     Append the first section of the main body of the art. This is done by going 
     through row 1 to 6 of a diamond with side-lengths of 3. The end characters are 
     appended for each row. In conclusion the diamond - parts generated are that of one 
     with a side-length of 3, and having 2 of them. 
    */ 
    for (int i = 1; i <= 6; i++) 
     builder.append('|').append(makeADiamondsStrip(3, 2, i)).append('|').append('\n'); 

    /* Append another divider, the same as the last. */ 
    builder.append(makeADivider(14, 2, 1)).append('\n'); 

    /* Create the next section of the body, this time with the bottom half then the top half of the diamonds; in that order. */ 
    for (int i = 4; i <= 6; i++) 
     builder.append('|').append(makeADiamondsStrip(3, 2, i)).append('|').append('\n'); 
    for (int i = 1; i <= 3; i++) 
     builder.append('|').append(makeADiamondsStrip(3, 2, i)).append('|').append('\n'); 

    /* Append the last divider. */ 
    builder.append(makeADivider(14, 2, 1)).append('\n'); 

    /* Append another triangle, this one being the same as the first. */ 
    builder.append(makeACenteredTriangle(5, 2, 1)); 

    /* Print out the final ASCII art. */ 
    System.out.println(builder.toString()); 
} 
} 

結果是:

 /**\  
    //**\\  
    ///**\\\ 
    ////**\\\\ 
/////**\\\\\ 
+=*=*=**=*=*=+ 
|../\..../\..| 
|./\/\../\/\.| 
|/\/\/\/\/\/\| 
|\/\/\/\/\/\/| 
|.\/\/..\/\/.| 
|..\/....\/..| 
+=*=*=**=*=*=+ 
|\/\/\/\/\/\/| 
|.\/\/..\/\/.| 
|..\/....\/..| 
|../\..../\..| 
|./\/\../\/\.| 
|/\/\/\/\/\/\| 
+=*=*=**=*=*=+ 
    /**\  
    //**\\  
    ///**\\\ 
    ////**\\\\ 
/////**\\\\\