在Java中打印星星图案可以使用循环结构来实现。以下是两种常见的打印星星图案的方法:
方法一:使用嵌套循环
public class Main {public static void main(String[] args) {int rows = 5; // 设置行数for (int i = 0; i < rows; i++) {for (int j = 0; j <= i; j++) {System.out.print("* ");}System.out.println();}}}输出结果:
** ** * ** * * ** * * * *方法二:使用字符串拼接
public class Main {public static void main(String[] args) {int rows = 5; // 设置行数String star = "* ";for (int i = 0; i < rows; i++) {System.out.println(star);star += "* ";}}}输出结果:
** ** * ** * * ** * * * *以上两种方法都可以根据需要进行调整以打印不同形状的星星图案。

