在 Java 中打印金字塔形的星星
Sheeraz Gul
2023年10月12日
基于模式的练习是学习 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
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
作者: Sheeraz Gul
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook