Java在处理Array时使用For循环来生成Pattern

时间:2017-05-29 05:26:19

标签: java arrays for-loop design-patterns

如果我想打印下面的图案,我有一个数组:

cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?)",(card_id,pin,account_id) )

这是我到目前为止所拥有的:

*******
 ***** 
  ***  
   *   
  ***  
 ***** 
*******

我肯定在某个地方出错了,我为我的无知道歉。我只是想学习。 谢谢!

5 个答案:

答案 0 :(得分:1)

试试这个:

int [] p = { 7, 5, 3, 1, 3, 5, 7 };

// as mentioned in the comment, you want < here and not <=
for (int i = 0; i < p.length; i++)
{
    // total number of spaces needed
    int numSpaces = p[0] - p[i];

    // this array will hold the '*'s
    char [] arr = new char[p[i]];

    // half the spaces for each side
    char [] spaces = new char [numSpaces / 2];

    // fill the arrays
    Arrays.fill(arr, '*');
    Arrays.fill(spaces, ' ');

    // build the string
    StringBuilder sb = new StringBuilder();
    sb.append(spaces);
    sb.append(arr);
    sb.append(spaces);

    System.out.println(sb);
}

<强>输出

*******
 ***** 
  ***  
   *   
  ***  
 ***** 
*******

答案 1 :(得分:1)

考虑以下解决方案,通过一次通过所有不同的星形模式来构建输出字符串。欣赏因为你的模式是对称的,我们只需要生成它的一半,因为另一半只是一个镜像。

String top = "";
String bottom = "";
int maxStars = 7;
for (int i=0; i < maxStars; i=i+2) {
    StringBuilder line = new StringBuilder();
    for (int j=0; j < i/2; ++j)          line.append(" ");  // left spaces
    for (int j=0; j < maxStars - i; ++j) line.append("*");  // stars
    for (int j=0; j < i/2; ++j)          line.append(" ");  // right spaces
    top += line + "\n";
    // this if check prevents us from adding the single star row twice
    if (maxStars - i > 1) {
        bottom = line + "\n" + bottom;
    }
}
String pattern = top + bottom;
System.out.println(pattern);

<强>输出:

*******
 ***** 
  ***  
   *   
  ***  
 ***** 
*******

在这里演示:

Rextester

答案 2 :(得分:1)

public static void main(String[] args) {

    int[] p = { 7, 5, 3, 1, 3, 5, 7 };

    for (int i = 0; i < p.length; i++) {
        for(int k=p[0]-p[i], m = 0;m<k;m++)    System.out.print(" ");
        for( int j = 0; j<p[i]; j++)           System.out.print(" *");
        System.out.println();
    }}

<强>输出

 * * * * * * *
   * * * * *
     * * *
       *
     * * *
   * * * * *
 * * * * * * *

答案 3 :(得分:0)

在这种情况下使用字符串重复:

String.join('', Collections.nCopies(p[i], '*'));

StringUtils.repeat('*', p[i]);

答案 4 :(得分:0)

尝试此逻辑可以很好地满足您的要求

public class Test {

public static void main(String[] args) {

int[] p = {7,5,3,1,3,5,7};

    for (int i = 0; i < p.length; i++) {

        int temp = p[i];

        if (i <= p.length/2) {

            for (int whiteSpace = 0; whiteSpace < i; whiteSpace++) {

                System.out.print(" ");

            }

        } else {

            for (int whiteSpace = p.length - i - 1; whiteSpace > 0; whiteSpace--) {

                System.out.print(" ");

            }


        }

        for (int j = 0; j < temp; j++) {

            System.out.print("*");

        }

        System.out.println("");



    }

}

**输出:**

*******
 *****
  ***
   *
  ***
 *****
*******