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

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

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

问题

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

**




**
*

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

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

  1. for(int a = 0; a < 8; a++) //1
  2. {
  3. if(a < 4){
  4. for(int b = a; b < 4; b++)
  5. {
  6. System.out.print("*");
  7. }
  8. }
  9. if(a >= 4)
  10. for(int c = a; c < 4; c++)
  11. {
  12. System.out.print("*");
  13. }
  14. System.out.println();
  15. } //loop 1
  16. }

}

  1. 这是我的输出:


**
*

  1. (输出后面有一些空格,我没有包含在内。这是因为外部循环迭代了八次。)如何使我的程序正确显示所有代码,而不仅仅是最后四行?
  2. 非常感谢您的帮助。
英文:

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:

  1. *
  2. **
  3. ***
  4. ****
  5. ***
  6. **
  7. *

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:

  1. public class Main
  2. {
  3. public static void main(String[] args) {
  4. for(int a = 0; a &lt; 8; a++) //1
  5. {
  6. if(a &lt; 4){
  7. for(int b = a; b &lt; 4; b++)
  8. {
  9. System.out.print(&quot;*&quot;);
  10. }
  11. }
  12. if(a &gt;= 4)
  13. for(int c = a; c &lt; 4; c++)
  14. {
  15. System.out.print(&quot;*&quot;);
  16. }
  17. System.out.println();
  18. } //loop 1
  19. }
  20. }

And here is my output:

  1. ****
  2. ***
  3. **
  4. *

(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 语句

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

  1. for (int a = 0; a < 7; a++) {
  2. if (a < 4) {
  3. for (int b = 0; b <= a; b++)
  4. System.out.print("*");
  5. } else {
  6. for (int c = a; c < 7; c++)
  7. System.out.print("*");
  8. }
  9. System.out.println();
  10. }

输出:

  1. *
  2. **
  3. ***
  4. ****
  5. ***
  6. **
  7. *

编辑:

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

  1. for (int a = 0; a < 4; a++) {
  2. for (int b = 0; b <= a; b++)
  3. System.out.print("*");
  4. System.out.println();
  5. }
  6. for (int a = 4; a <= 7; a++) {
  7. for (int b = a; b < 7; b++)
  8. System.out.print("*");
  9. System.out.println();
  10. }
英文:

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:

  1. for(int a = 0; a &lt; 7; a++) {
  2. if(a &lt; 4){
  3. for(int b = 0; b &lt;= a; b++)
  4. System.out.print(&quot;*&quot;);
  5. }else {
  6. for(int c = a; c &lt; 7; c++)
  7. System.out.print(&quot;*&quot;);
  8. }
  9. System.out.println();
  10. }

Output:

  1. *
  2. **
  3. ***
  4. ****
  5. ***
  6. **
  7. *

EDIT:

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

  1. for(int a = 0; a &lt; 4; a++) {
  2. for(int b = 0; b &lt;= a; b++)
  3. System.out.print(&quot;*&quot;);
  4. System.out.println();
  5. }
  6. for(int a = 4; a &lt;= 7; a++) {
  7. for(int b = a; b &lt; 7; b++)
  8. System.out.print(&quot;*&quot;);
  9. System.out.println();
  10. }

答案2

得分: 0

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

  1. int size = 4;
  2. for (int line = 1; line < size * 2; line++) {
  3. if (line <= size) {
  4. for (int i = 0; i < line; i++) {
  5. System.out.print("*");
  6. }
  7. } else {
  8. for (int i = 0; i < size * 2 - line; i++) {
  9. System.out.print("*");
  10. }
  11. }
  12. System.out.println();
  13. }
英文:

You're close. Try something like this:

  1. int size = 4;
  2. for(int line = 1; line &lt; size * 2; line++) {
  3. if(line &lt;= size) {
  4. for(int i = 0; i &lt; line; i++) {
  5. System.out.print(&quot;*&quot;);
  6. }
  7. }
  8. else {
  9. for(int i = 0; i &lt; size * 2 - line; i++) {
  10. System.out.print(&quot;*&quot;);
  11. }
  12. }
  13. System.out.println();
  14. }

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:

确定