2017-04-03 109 views
-4
 expected output:   **** my output:  **** 
           ***     *** 
           **      ** 
           *      * 
           **      * 
           ***     * 
           ****     **** 

我的代码:如何在三角形中打印*?

class assign17 
    { 
     public static void main(String[] args) 
     { 
      int n=7; 
      for(int i=0;i<7;i++) 
      { 
       for(int j=0;j<4;j++)  
       { 
        if(i+j<=n-4||j==0||i==n-1) 
         System.out.print("*"); 
        else 
         System.out.print(" "); 
       } 
       System.out.println(); 
      } 
     } 
    } 

如何让这个样子? 如何选择内部部分? 是我必须使用另一个for循环,或者我可以让自己进入。

回答

0

它可能不是最好的解决方案,但它的工作原理;)我只是从4星开始,每行减少1个。如果只剩下1星,我会再次增加星星。

public static void main(String[] args) { 
     int stars = 4; 
     boolean lower = true; 
     for(int i=0;i<7;i++) 
     { 
      for(int j=0;j<stars;j++) { 
       System.out.print("*"); 
      } 
      if(lower) { 
       stars--; 
       if(stars==0){ 
        stars=2; 
        lower = false; 
       } 
      } 
      else { 
       stars++; 
      } 

      System.out.println(); 
     } 
    } 
0
public class numPtr { 
public static void main(String arg[]){ 
int ck=0,c=2; 
    while(c>0){ 
    if(ck==0){ 
     for(int i=1,r=5;i<=5;i++,r--){ 
     for(int j=1;j<=r;j++){ 
      System.out.print(j); 
     } 
     System.out.println();  } 
     ck++; 
    } else{ 
     for(int i=2;i<=5;i++){ 
     for(int j=1;j<=i;j++){ 
      System.out.print(j);   } 
     System.out.println(); 
    } 
    } 
    c--; 
    } 
} 
} 

它会给你像图案输出..

12345 
1234 
123 
12 
1 
12 
123 
1234 
12345 
2

这会给你期望的输出

public static void main(String[] args) 
{ 

    for (int row=1;row<=7;row+=2){ 
     for (int space=7;space>=row;space-=2){ 
      System.out.print("*"); 
     } 
     for (int i=1;i<=row;i++){ 
      System.out.print(" "); 
     } 

     System.out.print("\n"); 
    } 

    for (int row=5;row >=1; row-=2){ 
     for (int space=7;space>=row;space-=2){ 
       System.out.print("*"); 
     } 
     for (int i=1;i<=row;i++){ 
      System.out.print(" "); 
     } 

     System.out.print("\n"); 
    } 

} 

我让你四为,它看起来像功课所以我猜你更有可能这样理解它。

说明:

两个第一循环将打印你的星星,直到这个

**** 
*** 
** 
* 

而其他两个循环将打印恒星的最后一部分

** 
*** 
**** 
1

如果你想如果我正确的话,你可以用递归的方法做到这一点

static void printstars(int num){ 
    if(num > 0){ 
     for(int t = 0; t<num;t++) 
      System.out.print("*"); 
     System.out.println(); 
     printstars(num -1); 
     for(int t = 0; t<num;t++) 
      System.out.print("*"); 
     System.out.println(); 
    } 
} 
2

试试这个,我已经加入否则,如果条件代码

public static void main(String[] args) { 
    int n = 7; 
    for (int i = 0; i < 7; i++) { 
     for (int j = 0; j < 4; j++) { 
      if (i + j <= n - 4 || j == 0 || i == n - 1) 
       System.out.print("*"); 
      else if (i - j >= n - 4) 
       System.out.print("*"); 
      else 
       System.out.print(" "); 
     } 
     System.out.println(); 
    } 
} 
+0

谢谢兄弟!你是唯一一个让我容易理解的答案 –

-1

你可以做这样的事情。提取出使用的字符,所以如果将来需要更改它。此外,请注意System.out.println仅在第一个for循环中,因此它不那么复杂。有一种方法负责构建您需要打印的字符串。星星数量有两个计数器,当您达到零时,您切换y变量并开始计数。

public static final String CHARACTER = "*"; 

    public static void main(String args[]) { 

    int x = 4; 
    int y = 1; 
    boolean hitZero = false; 

    for (int i = 0; i < 7; i++) { 
     System.out.println(buildStr(hitZero?y:x)); 

     if(x==1) 
      hitZero = true; 

     if(hitZero) 
     y++; 
     else 
     x--; 
    } 
    } 

    private static String buildStr(int count) { 
    String res = ""; 
    for(int i=0; i< count; i++) 
     res+=CHARACTER; 
    return res; 
    } 

输出

**** 
*** 
** 
* 
** 
*** 
**** 
1

星的数量应该打印每次迭代

iteration(i)  => 0 | 1 | 2 | 3 | 4 | 5 | 6 
number of star => 4 | 3 | 2 | 1 | 2 | 3 | 4 
Math.abs(mid-i)+1 => 4 | 3 | 2 | 1 | 2 | 3 | 4 

所以我在这里已经使用内循环的限制Math.abs(mid-i)+1

public static void main(String[] args) 
{ 
    int n = 7; 
    int mid = n/2; 

    for(int i=0; i<n; i++) 
    { 
    for(int j=0; j< Math.abs(mid-i)+1; j++) 
    { 
     System.out.print("*"); 
    } 
    System.out.println(); 
    } 
} 

输出:

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