Frage

My program that prints out a diamond like this:

...............*
..........*    *    *
.....*    *    *    *    *
*    *    *    *    *    *    *
.....*    *    *    *    *
..........*    *    *    
...............*

But it only works if the parameter or each side of the diamond is 4. For example if I input 6, the spacing on the bottom triangle is wrong and I been trying to figure it out.

The bottom triangle doesn't change when the parameter is changed, only the top one does. It only works for input 4.

public static void printMoreStars(int n) {
    int rowsPrime = 0;

    for (int i = n + 1; i > 0; i--) {
        for (int j = 0; j < (2 * i) - 1; j++) {
            System.out.print(" ");
        }
        for (int d = 0; d < (2 * rowsPrime) - 1; d++) {
            System.out.print("*" + " ");
        }
        System.out.println(); //new line character

        rowsPrime += 1;
        System.out.println(" ");
    }

    //bottom triangle
    for (int i = 1; i < n + 1; i++) {
        for (int j = 0; j < (2 * i) + 1; j++) {
            System.out.print(" ");
        }
        for (int d = 0; d < rowsPrime; d++) {
            System.out.print("*" + " ");
        }
        System.out.println(); //new line character

        rowsPrime -= 2;
        System.out.println(" ");
    }
}
War es hilfreich?

Lösung

You made two mistakes when using rowPrimes. See my annotations below:

public class Stars {
    public static void main(String[] args) {
        printMoreStars(Integer.parseInt(args[0]));
    }

    public static void printMoreStars(int n) {
        int rowsPrime = 0;

        for (int i = n + 1; i > 0; i--) {
            for (int j = 0; j < (2 * i) - 1; j++) {
                System.out.print(" ");
            }
            for (int d = 0; d < (2 * rowsPrime) - 1; d++) {
                System.out.print("*" + " ");
            }
            System.out.println();   //new line character

            rowsPrime += 1;
            System.out.println(" ");
        }

        rowsPrime -= 2; // <- middle line is already printed, so skip that

        //bottom triangle
        for (int i = 1; i < n + 1; i++) {
            for (int j = 0; j < (2 * i) + 1; j++) {
                System.out.print(" ");
            }
            for (int d = 0; d < (2 * rowsPrime) - 1; d++) { // <- changed condition to be the same as above
                System.out.print("*" + " ");
            }
            System.out.println();   //new line character

            rowsPrime--; // <- you have to decrease rowPrime by one.
            System.out.println(" ");
        }
    }
}

Andere Tipps

To print a rhombus with dots on the left, you can use this code.

Try it online!

public static void main(String[] args) {
    printRhombus(3);
}
public static void printRhombus(int n) {
    for (int i = -n; i <= n; i++) {
        // last element in the row
        int last = n - Math.abs(i);
        for (int j = -n; j <= last; j++)
            if (Math.abs(i) + Math.abs(j) <= n)
                System.out.print("*" + (j < last ? "    " : ""));
            else
                System.out.print(".....");
        System.out.println();
    }
}

Output:

...............*
..........*    *    *
.....*    *    *    *    *
*    *    *    *    *    *    *
.....*    *    *    *    *
..........*    *    *
...............*

See also: Print a diamond shape with Java

Check This:

import java.io.*;
import java.lang.*;
import java.util.*;

class DiamondPattern {
    static public int ReadInteger() {
        try {
            String inpString = "";
            InputStreamReader input = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(input);
            String s = reader.readLine();
            return Integer.parseInt(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println("Program for displaying pattern of *.");
        System.out.print("Enter the maximum number of *: ");
        int n = ReadInteger();
        System.out.println("\nHere is the Diamond of Stars\n");
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < (n - i); j++)
                System.out.print(" ");
            for (int j = 1; j <= i; j++)
                System.out.print("*");
            for (int k = 1; k < i; k++)
                System.out.print("*");
            System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = 0; j < (n - i); j++)
                System.out.print(" ");
            for (int j = 1; j <= i; j++)
                System.out.print("*");
            for (int k = 1; k < i; k++)
                System.out.print("*");
            System.out.println();
        }
        System.out.println();
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top