Chris Nedworth
Chris Nedworth

Reputation: 31

Diamond-printing program in Java creating an infinite loop

I'm having some problems with printing a filled diamond. The program incorrectly formats the diamond.

This code:

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

prints out this:

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

Edit: I've got the top half of the diamond done, but I can't seem to get the bottom half down. It creates an infinite loop.

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

EDIT 2: Fixed the infinite loop, but now the stars are unaligned:

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

OUTPUT:

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

EDIT 3: Got the stars aligned, just need to get rid of middle row.

code:

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

Output:

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

Upvotes: 0

Views: 834

Answers (4)

Nikhil Mugganawar
Nikhil Mugganawar

Reputation: 11

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

    }
}

Upvotes: 1

ASCIIThenANSI
ASCIIThenANSI

Reputation: 893

Your original code has a few problems.

Firstly, you're using this:

System.out.println("*")

instead of this:

System.out.print("*")

In Java, println and print are two different things. println automatically adds a newline at the end of the string, whereas print does not.

However, you can't just change println to print, since then all the output will be on one line. Every time you run through the loop, use System.out.println() to create a newline.

You also don't define rows, spaces, and stars (I'm assuming they aren't defined elsewhere.) You can define them in the for loop, like so:

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

As for the bottom half of the diamond, you can simply reverse the for loop and leave out one line (the middle line, since that was created by the first loop.) Here is the start of the second for loop, the contents are the same as the first:

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

Instead of starting at 0, adding 2 and continuing until we reach height, we start at height - 2, subtract 2, and continue until we reach 0.
Why height - 2 and not height? That's because if we use height, it will print a duplicate row. height - 2 removes one row (the duplicate) and prints the rest of the bottom half.

Here is the complete fixed code:

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

Let me know if anything is inaccurate or unclear.

Upvotes: 1

Angzuril
Angzuril

Reputation: 413

For your updated question, you get an infinite loop because your for loop

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

only decrements rows (from 1), and checks for less than.

Try something like:

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

Upvotes: 1

Rahman
Rahman

Reputation: 3785

Code :

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

Output :

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

Upvotes: -2

Related Questions