public static void main(String[] args) {
int rows = 0; // The number of digits needed for the diamond
ArrayList<Integer> list = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.print("Please enter * number:");
rows = input.nextInt();
for (int k = 0; k < rows; k++) {
if (k % 2! =0) {
// Find all possible lines that are not small
int count = k;
// Calculate the number of * numbers needed to draw the rhombus
for (int l = 1; l < k; l = l + 2) {
count = count + 2 * l;
}
// The total number of lines smaller than the input
if (count < rows) {
list.add(k);
}
}
}
System.out.println("Draw the most diamonds");
System.out.println();
int sum = 0;
// The last line in the set is the maximum number of lines that can be drawn as diamonds
int n = (list.get(list.size() - 1) + 1) / 2;
for (int i = 1; i <= n; i++) {The outer loop variable I controls the number of rows
for (int j = 1; j <= n - i; j++) {// The inner loop variable j controls the number of Spaces in the line
System.out.print("");
}
for (int p = 1; p <= 2 * i - 1; p++) {// the inner loop variable k controls the number of lines *
System.out.print("*");
sum++;
}
System.out.print("\n");
}
// Print the lower part of the diamond
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
System.out.print("");
}
for (int q = 1; q <= 2 * i - 1; q++) {
System.out.print("*");
sum++;
}
System.out.print("\n");
}
System.out.println("Remaining * number :" + (rows - sum));
}
Copy the code
Result of operation