2

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

3 回答 3

2

你在使用时犯了两个错误rowPrimes。请参阅下面的注释:

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(" ");
        }
    }
}
于 2013-10-01T06:13:06.757 回答
1

要在左侧打印带点的菱形,您可以使用此代码。

在线尝试!

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

输出:

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

另请参阅:使用 Java 打印菱形

于 2021-07-03T23:36:09.440 回答
0

检查这个:

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();
    }
}
于 2013-10-01T06:06:03.147 回答