在Java中,如何使用嵌套的for循环编写一个星号金字塔?

huangapple go评论121阅读模式
英文:

In Java, how would I code a pyramid of asterisks by using nested for loops?

问题

我正在处理一个作业,需要使用嵌套循环编写一个侧倾的星号金字塔程序。
这个程序的输出应该类似这样:

**




**
*


当我运行我的程序时,它只显示代码的最后四行。我不知道前面的三行为什么没有显示出来。
这是我的代码:

public class Main
{
public static void main(String[] args) {

    for(int a = 0; a < 8; a++) //1
    {
        if(a < 4){
            for(int b = a; b < 4; b++)
            {
                System.out.print("*");
            }
            
        }
        if(a >= 4)
            for(int c = a; c < 4; c++)
            {  
                System.out.print("*");
            }
        
        System.out.println();
    } //loop 1
    
}

}


这是我的输出:


**
*

(输出后面有一些空格,我没有包含在内。这是因为外部循环迭代了八次。)如何使我的程序正确显示所有代码,而不仅仅是最后四行?

非常感谢您的帮助。
英文:

I am working on an assignment where I have to use nested loops to program a pyramid of asterisks that is on its side.
The output of this program should look like this:

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

When I run my program, it only displays the final four lines of the code. I don't know why the first three don't show up.
Here is my code:

public class Main
{
	public static void main(String[] args) {
		
		for(int a = 0; a &lt; 8; a++) //1
		{
		    if(a &lt; 4){
		        for(int b = a; b &lt; 4; b++)
		        {
		            System.out.print(&quot;*&quot;);
		        }
		        
		    }
		    if(a &gt;= 4)
		        for(int c = a; c &lt; 4; c++)
		        {  
		            System.out.print(&quot;*&quot;);
		        }
		    
		    System.out.println();
		} //loop 1
		
	}
}

And here is my output:

****
***
**
*

(There is some empty space after the output that I did not include. This is caused by the outer for loop iterating eight times.) How would I make my program correctly display all of the code, instead of just the last four lines?

Any help would be greatly appreciated.

答案1

得分: 1

在你的逻辑中有几个错误:

  1. 由于你只需要 7 行,第一个循环应该迭代直到 a < 7
  2. 在前 3 行中,你的嵌套循环应该从 0 迭代到 a
  3. 此后,另一个嵌套循环应该从 a 迭代到 7
  4. 最好使用 if-else 语句替代两个 if 语句

以下是我测试过的完整解决方案:

for (int a = 0; a < 7; a++) {
     if (a < 4) {
          for (int b = 0; b <= a; b++)
               System.out.print("*");
     } else {
          for (int c = a; c < 7; c++)
               System.out.print("*");
     }
     System.out.println();
}

输出:

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

编辑:

正如在评论中提到的,你还可以将外部循环拆分成两部分,以消除条件,代码如下:

for (int a = 0; a < 4; a++) {
     for (int b = 0; b <= a; b++)
          System.out.print("*");
     System.out.println();
}
for (int a = 4; a <= 7; a++) {
	 for (int b = a; b < 7; b++)
	   	  System.out.print("*");
	 System.out.println();
}
英文:

There are several mistakes in your logic:

  1. Since you only need 7 rows, the first loop should iterate until a &lt; 7
  2. In the first 3 rows, your nested loop should iterate from 0 to a
  3. After that, the other nested loop should go from a to 7
  4. It is better to use if-else instead of two if statements

Here is the complete solution that I tested:

for(int a = 0; a &lt; 7; a++) {
     if(a &lt; 4){
          for(int b = 0; b &lt;= a; b++)
               System.out.print(&quot;*&quot;);
     }else {
          for(int c = a; c &lt; 7; c++)
               System.out.print(&quot;*&quot;);
     }
     System.out.println();
}

Output:

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

EDIT:

As mentioned in the comments, you can also split the outer loop into two parts in order to remove the conditions as follows:

for(int a = 0; a &lt; 4; a++) {
     for(int b = 0; b &lt;= a; b++)
          System.out.print(&quot;*&quot;);
     System.out.println();
}
for(int a = 4; a &lt;= 7; a++) {
	 for(int b = a; b &lt; 7; b++)
	   	  System.out.print(&quot;*&quot;);
	 System.out.println();
}

答案2

得分: 0

你很接近了。尝试类似这样的代码:

int size = 4;

for (int line = 1; line < size * 2; line++) {
  if (line <= size) {
    for (int i = 0; i < line; i++) {
      System.out.print("*");
    }
  } else {
    for (int i = 0; i < size * 2 - line; i++) {
      System.out.print("*");
    }
  }
  System.out.println();
}
英文:

You're close. Try something like this:

int size = 4;
		
for(int line = 1; line &lt; size * 2; line++) {
  if(line &lt;= size) {
    for(int i = 0; i &lt; line; i++) {
      System.out.print(&quot;*&quot;);
    }
  }
  else {
    for(int i = 0; i &lt; size * 2 - line; i++) {
      System.out.print(&quot;*&quot;);
    }
  }
  System.out.println();
}

huangapple
  • 本文由 发表于 2020年5月30日 05:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/62095080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定