總體分析:
首先,不論打印平面圖形或者3D圖形,都是採用分層打印的方法;對於程序打印平面規律圖形來說,可以利用雙重循環進行打印,外層循環控制打印的層數,內層循環控制每一層打印的內容,當打印完一層後,就換行打印下一層。
1:打印實心鑽石
代碼如下:
package net.algchallenge;
import java.util.Scanner;
public class PrintSolidDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//注意:因爲是同一行要打印多個空白和星星,所以要利用System.out.print();然後當打印了鑽石的一行的時候就啓動換行,不然所有的空白和星星都在同一行裏
while (true) {
System.out.print("row = ");
int row = sc.nextInt();//定義鑽石的行數
if (row % 2 == 0) {//如果鑽石的行數爲偶數
row++;//鑽石行數爲偶數時,行數+1,就可以實現行數爲奇數時的上下對稱情況
for (int i = 1; i <= row; i++) {
if (i <= row / 2) {
for (int j = 1; j <= row / 2 - i + 1; j++) {//打印空白:當打印的是鑽石的上層時,空白的個數成遞減趨勢,所以要使用row - i的形式
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");//打印星星:當打印的是鑽石上層的時候,星星個個數成遞增趨勢,並且每一行的個數爲2*i-1
}
System.out.println();
} else {//打印下層的星星和空白,可根據上層推導而來
for (int j = 1; j <= i - (row / 2 + 1); j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (row - i) + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
} else {//如果鑽石的行數爲奇數
for (int i = 1; i <= row; i++) {
if (i <= row / 2) {
for (int j = 1; j <= row / 2 - i + 1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
} else {
for (int j = 1; j <= i - (row / 2 + 1); j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (row - i) + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
}
}
效果如圖:
2:打印空心鑽石
分析:打印空心鑽石相比實心鑽石沒有太大區別,只是在打印每一層具體內容的時候,加了一些判斷條件。
代碼如下:
package net.algchallenge;
import java.util.Scanner;
public class PrintHollowDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("row = ");
int row = sc.nextInt();
if (row % 2 != 0) {
for (int i = 1; i <= row; i++) {
if (i <= row / 2) {
for (int j = 1; j <= row / 2 - i + 1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
if (i > 1 && i <= row / 2) {
if (j > 1 && j < 2 * i - 1) {
System.out.print(" ");
} else {
System.out.print("*");
}
} else {
System.out.print("*");
}
}
System.out.println();
} else {
for (int j = 1; j <= i - (row / 2 + 1); j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (row - i) + 1; j++) {
if (i >= row / 2 + 1 && i < row) {
if (j > 1 && j < 2 * (row - i) + 1) {
System.out.print(" ");
} else {
System.out.print("*");
}
} else {
System.out.print("*");
}
}
System.out.println();
}
}
} else {
row++;
for (int i = 1; i <= row; i++) {
if (i <= row / 2) {
for (int j = 1; j <= row / 2 - i + 1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
if (i > 1 && i <= row / 2) {
if (j > 1 && j < 2 * i - 1) {
System.out.print(" ");
} else {
System.out.print("*");
}
} else {
System.out.print("*");
}
}
System.out.println();
} else {
for (int j = 1; j <= i - (row / 2 + 1); j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (row - i) + 1; j++) {
if (i >= row / 2 + 1 && i < row) {
if (j > 1 && j < 2 * (row - i) + 1) {
System.out.print(" ");
} else {
System.out.print("*");
}
} else {
System.out.print("*");
}
}
System.out.println();
}
}
}
}
}
}
效果如圖: