Java 中的钻石打印程序创建无限循环

Diamond-printing program in Java creating an infinite loop

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

此代码:

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

输出:

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

代码:

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

输出:

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

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

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

仅递减行(从 1 开始),并检查小于。

试试这样的东西:

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

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

首先,您使用的是:

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

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

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

    }
}