2017-05-29 89 views
1

如果我想打印下面的模式,我有一个数组:的Java使用For循环,同时处理阵列,使模式

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

这是我到目前为止有:

public static void main(String[] args) { 
    int[] p = {7,5,3,1,3,5,7}; 

    for (int i=0; i <= 7; i++) { 
     System.out.print("\n"); 
     for (int k =i; k <= 7; k++) { 
      k = p[i]; 
      // I'm trying to find a way to print "*"s with array element value 
      for(int j=i; j <= 7; j++) { 
       System.out.print("*"); 
      } 
     } 
    } 
} 

我绝对出错了,我为我的无知道歉。我只是想学习。 谢谢!

+1

你应该使用 '<' 的循环,而不是 '<=',因为第一你有7个元素从0到6而不是8到0到7 :) – Rjiuk

+0

个人,我不会将值存储在数组中。这样想一下,如果'stars = 7',那么'spaces = 0'。当星星减少2时,空间增加1。直到星星为1,然后相反。 –

+0

@MichaelMarkidis你来自C背景吗?在Java中,通常我们将开头括号放在与构造相同的行上。 –

回答

0

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

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

StringUtils.repeat('*', p[i]); 
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); 
} 

输出

******* 
***** 
    *** 
    * 
    *** 
***** 
******* 
+0

谢谢!这很容易理解。 –

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

+0

天才。我喜欢。 –

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(""); 



    } 

} 

**输出:**

******* 
***** 
    *** 
    * 
    *** 
***** 
******* 
+1

我喜欢你如何申报临时工。这非常方便,我一定会在将来使用它。 –

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

输出

* * * * * * * 
    * * * * * 
    * * * 
     * 
    * * * 
    * * * * * 
* * * * * * * 
+1

你的'外''循环有错误的界限,但我纠正了+1 –

+0

@Tim Beigeleisen谢谢你的纠正。 –

+0

我是初学者,所以我非常喜欢这个解决方案。对我来说很容易理解。谢谢! –