HOWTO · Java
在 Java 中列印金字塔形的星星
本教程演示如何在 Java 中列印金字塔星星。
本頁內容
基於模式的練習是學習 Java 中巢狀迴圈的好方法,列印金字塔結構是進行這些練習的最佳示例。
本教程演示如何在 Java 中列印不同的星形金字塔。
用 Java 列印星星金字塔
按照以下步驟在 Java 中列印星形金字塔。
-
決定行數。
-
外迴圈的第一次迭代將列印金字塔圖案的第一行。
-
外部迴圈內部將有兩個迴圈:第一個迴圈列印每一行的空格,另一個迴圈列印金字塔每一行的星星。
讓我們實現這些步驟:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int pyramid_spaces;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// This loop will print one row in every iteration
for (x = 0; x < pyramid_rows; x++) {
// this loop will print the spaces
for (pyramid_spaces = x; pyramid_spaces < pyramid_rows; pyramid_spaces++) {
System.out.print(" ");
}
// this loop will print the stars
for (star_count = 0; star_count < (x + 1); star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
上面的程式碼將要求使用者輸入金字塔的行數並相應地列印行。
輸出:
Enter the number of rows of pyramid: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
在 Java 中列印星的倒金字塔
現在,讓我們嘗試列印一個倒金字塔形的星星。我們可以通過反轉上面程式碼中描述的過程來列印倒金字塔。
例子:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int pyramid_spaces;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// This loop will print one row in every iteration
for (x = 0; x < pyramid_rows; x++) {
for (pyramid_spaces = 0; pyramid_spaces < x; pyramid_spaces++) {
System.out.print(" ");
}
for (star_count = x; star_count < pyramid_rows; star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
此程式碼將通過從使用者那裡獲取輸入行來列印倒金字塔。
輸出:
Enter the number of rows of pyramid: 7
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
在 Java 中同時列印原始和倒置的雙星金字塔
雙星金字塔將建立一個類似鑽石的形狀。
例子:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int pyramid_spaces;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// Loops for the first pyramid
for (x = 0; x < pyramid_rows; x++) {
for (pyramid_spaces = x; pyramid_spaces < pyramid_rows; pyramid_spaces++) {
System.out.print(" ");
}
for (star_count = 0; star_count < (x + 1); star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
// Loops for the inverted pyramid
for (x = pyramid_rows; x > 0; x = (x - 1)) {
for (pyramid_spaces = pyramid_rows; pyramid_spaces >= (x - 1); pyramid_spaces--) {
System.out.print(" ");
}
for (star_count = (x - 1); star_count > 0; star_count--) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
輸出:
Enter the number of rows of pyramid: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
在 Java 中列印半個金字塔的星星
要列印半金字塔,我們只需要兩個迴圈。
例子:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// This loop will print one row in every iteration
for (x = 0; x < pyramid_rows; x++) {
for (star_count = 0; star_count <= x; star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
輸出:
Enter the number of rows of pyramid: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
在 Java 中列印倒置的半金字塔形的星星
通過更改上面程式碼中的第二個迴圈,我們可以在 Java 中列印倒置的半金字塔。
例子:
package delftstack;
import java.util.*;
public class Pyramid_Stars {
public static void main(String[] args) {
int pyramid_rows;
int x;
int star_count;
Scanner enter_rows = new Scanner(System.in);
System.out.print("Enter the number of rows of pyramid: ");
pyramid_rows = enter_rows.nextInt();
// This loop will print one row in every iteration
for (x = 0; x < pyramid_rows; x++) {
for (star_count = x; star_count < pyramid_rows; star_count++) {
System.out.print("* ");
}
System.out.print("\n");
}
}
}
輸出:
Enter the number of rows of pyramid: 7
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*