【问题标题】:Diamond-printing program in Java creating an infinite loopJava中的钻石打印程序创建一个无限循环
【发布时间】:2015-10-01 13:34:11
【问题描述】:

我在打印填充钻石时遇到了一些问题。程序错误地格式化了菱形。

这段代码:

      for (rows = 1; rows < height; rows += 2){

            for (spaces =0; spaces < height - 1 - rows / 2; spaces++){
                System.out.print(" ");
            }

            for (stars = 0; stars < rows; stars++){
                System.out.println("*");
            }
        }

打印出来:

Please enter a positive odd height for the diamond:
9
    *
   *
*
*
  *
*
*
*
*
 *
*
*
*
*
*
*

编辑:我已经完成了钻石的上半部分,但我似乎无法将下半部分放下。它创建了一个无限循环。

for (rows = 1; rows < height  + 1 ; rows += 2){

            for (spaces =0; spaces < height - 1 - rows / 2; spaces++){
                System.out.print(" ");
            }

            for (stars = 0; stars < rows; stars++){
                System.out.print("*");
            }
            System.out.println("");
        }

        for (rows = 1; rows < height; rows -= 2){

            for (spaces =0; spaces < height; spaces++){
                System.out.print(" ");
            }

            for (stars = 0; stars < rows; stars++){
                System.out.print("*");
            }
            System.out.println("");
        }

编辑 2:修复了无限循环,但现在星星未对齐:

for (rows = 1; rows < height  + 1 ; rows += 2){

            for (spaces =0; spaces < height - 1 - rows / 2; spaces++){
                System.out.print(" ");
            }

            for (stars = 0; stars < rows; stars++){
                System.out.print("*");
            }
            System.out.println("");
        }

        for (rows = height - 2; rows >= 1; rows -= 2){
                for (spaces =0; spaces < height; spaces++){
                    System.out.print(" ");
                }

                for (stars = 0; stars < rows; stars++){
                    System.out.print("*");
                }
                System.out.println("");
            }

输出:

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

编辑 3: 星星对齐了,只需要去掉中间一排。

代码:

for (rows = 1; rows < height  + 1 ; rows += 2){

            for (spaces =0; spaces < height - 1 - rows / 2; spaces++){
                System.out.print(" ");
            }

            for (stars = 0; stars < rows; stars++){
                System.out.print("*");
            }
            System.out.println("");
        }

        for ( rows = height; rows > 0; rows -= 2){

            for ( spaces =0; spaces < height - 1 - rows / 2; spaces++){
                System.out.print(" ");
            }

            for ( stars = 0; stars < rows; stars++){
                System.out.print("*");
            }

            System.out.println();
        }

输出:

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

【问题讨论】:

  • 发布预期的输出。
  • 您的第二个内部循环应该使用print 而不是println。在第二个内循环结束后放置println
  • 我已经用固定代码更新了我的答案。它应该去掉中间的重复行。

标签: java loops for-loop infinite-loop


【解决方案1】:

您的原始代码有一些问题。

首先,你正在使用这个:

System.out.println("*")

而不是这个:

System.out.print("*")

在 Java 中,printlnprint 是两个不同的东西。 println 自动在字符串末尾添加换行符,而 print 不会。

但是,您不能只将println 更改为print,因为这样所有的输出都将在一行上。每次运行循环时,使用System.out.println() 创建一个换行符。

您也没有定义 rowsspacesstars(我假设它们没有在其他地方定义。)您可以在 for 循环中定义它们,如下所示:

for (int rows = 1; rows < height; rows += 2) {

至于菱形的下半部分,您可以简单地反转 for 循环并省略一行(中间行,因为它是由第一个循环创建的。)这是第二个 for 循环的开始,内容同第一个:

for (int rows = height - 2; rows > 0; rows -= 2){

我们不是从 0 开始,加 2 并继续直到达到 height,而是从 height - 2 开始,减 2,然后继续直到达到 0。
为什么是height - 2 而不是height?那是因为如果我们使用height,它会打印一个重复的行。 height - 2 删除一行(重复)并打印下半部分的其余部分。

这里是完整的固定代码:

    for (int rows = 1; rows < height; rows += 2){

        for (int spaces =0; spaces < height - 1 - rows / 2; spaces++){
            System.out.print(" ");
        }

        for (int stars = 0; stars < rows; stars++){
            System.out.print("*");
        }

        System.out.println();
    }

        for (int rows = height - 2; rows > 0; rows -= 2){

        for (int spaces = 0; spaces < height - rows / 2 - 1; spaces++) {
            System.out.print(" ");
        }

        for (int stars = 0; stars < rows; stars++){
            System.out.print("*");
        }

        System.out.println();
    }

如果有任何不准确或不清楚的地方,请告诉我。

【讨论】:

  • 我还是两次中线。
  • @ChrisNedworth 在第二个for 循环中,尝试将int rows = height 替换为int rows = height - 1
  • 那就更糟了。线条变得不对齐,它摆脱了最后一行。
  • @ChrisNedworth 我用固定版本的代码编辑了我的帖子,现在应该可以使用了。
【解决方案2】:

对于您更新的问题,您会得到一个无限循环,因为您的 for 循环

for (rows = 1; rows < height; rows -= 2)

仅减少行数(从 1 开始),并检查是否小于。

尝试类似:

for (rows = height; rows >= 1; rows -= 2)

【讨论】:

    【解决方案3】:
    import java.util.Scanner;
    
    
    public class diamond {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            int i,n,j,k,l=0;
            System.out.println("enter number n");
            Scanner in=new Scanner(System.in);
            n=in.nextInt();
    
            for(i=1;i<=n;i++)
            {
                for(j=0;j<=n-i;j++)
                {
                    System.out.print(" ");
                }
                for(k=1;k<=i;k++)
                {
                System.out.print("*");
    
                }
                for(l=2;l<=i;l++)
                {
                    System.out.print("*");
    
                }
                System.out.println();
            }
            for(i=n;i>=1;i--)
            {
                for(j=0;j<=n-i;j++)
                {
                    System.out.print(" ");
                }
                for(k=1;k<=i;k++)
                {
                    System.out.print("*");
                }
                for(k=2;k<=i;k++)
                {
                    System.out.print("*");
    
                }
                System.out.println();
            }
    
        }
    }
    

    【讨论】:

    • 以防万一,如果您希望接受用户的输入,只需稍作改动,代码就会如上所示
    • 你写的评论应该是答案的一部分。请编辑并阅读Minimal, Complete and verifiable example 以改进您未来的答案。
    • 感谢Gala,一定会照顾好它,这里有点新,习惯了。
    • 没问题!看看帮助中心,你会发现很多关于如何提问/回答的有用帮助:)
    【解决方案4】:

    代码:

    for (int i = 1; i < 10; i += 2) {
          for (int j = 0; j < 9 - i / 2; j++)
            System.out.print(" ");
    
          for (int j = 0; j < i; j++)
            System.out.print("*");
    
          System.out.print("\n");
        }
    
        for (int i = 7; i > 0; i -= 2) {
          for (int j = 0; j < 9 - i / 2; j++)
            System.out.print(" ");
    
          for (int j = 0; j < i; j++)
            System.out.print("*");
    
          System.out.print("\n");
        }
    

    输出:

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

    【讨论】:

      猜你喜欢
      • 2022-11-21
      • 2021-09-17
      • 2015-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多