如何在java中打印星形图案

如何在java中打印星形图案,java,Java,我试图找到任何序列或公式为下面的星星模式,但我没有找到,所以简单地尝试下面 public class MyClass { public static void main(String args[]) { System.out.println("....*...."); System.out.println("...***..."); System.out.println("*********"); System.out.

我试图找到任何序列或公式为下面的星星模式,但我没有找到,所以简单地尝试下面

public class MyClass {
    public static void main(String args[]) {


        System.out.println("....*....");
        System.out.println("...***...");
        System.out.println("*********");
        System.out.println(".*******.");
        System.out.println("*********");
        System.out.println("...***...");
        System.out.println("....*....");

    }
}
在这里。表示空格字符。 我们能用loop打印这颗星吗


是的,它可以使用循环来完成,而不应该通过sout语句来完成。 看起来你是编程的初学者,所以我建议你自己尝试一下这类问题的逻辑。这将有助于你的成长

一行一行地试一下。 这些问题通常需要两个循环,一个在垂直方向移动,另一个在水平方向移动。 你可以把它想象成一个i*j矩阵,比如3X3,4X4

所以你自己试试,从一些基本的开始。
如果您仍然需要解决方案,请ping我,但请先自己尝试。

不确定您的意思,但如果您确实想要循环,这里有一个解决方案

public class Star {

    public static void main(String[] args) {

        int[] dots = new int[] {4, 3, 0, 1, 0, 3, 4};
        int width= 9;

        for (int i = 0; i < dots.length; i++) {
            for (int j = 0; j < width; j++) {
                if (j < dots[i] || j > width - dots[i] - 1) {
                    System.out.print(".");
                } else {
                    System.out.print("*");
                }
            }
            System.out.println();
        }

    }
}

希望有帮助!这对于低年级学生来说很容易理解,而不是创建数组

class starPattern {
    public static void main(String []args) {
        for(int i=1;i<=7;i++) {
            for(int j=1;j<=9;j++) {
                if(i==3 || i==5 || j==5 || (i==4 && j>1 && j<9) || ((i==2 || i==6) && (j>3 && j<7))) {
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}


我正在使用Java11。你应该能够画出任何对映体的星号图案。详细解释请阅读评论

import java.util.Arrays;
class Star {
    public static void main(String[] args) {
        int[] stars = new int[] { 1, 3, 9, 7, 9, 3, 1 }; // asterisks per line
        int max = Arrays.stream(stars).max().getAsInt(); // max asterisks in any line
        for (int i = 0; i < stars.length; i++) { // prints the asterisks for a given row, and pads it left and right with spaces
            System.out.print(" ".repeat((max - stars[i]) / 2) + "*".repeat(stars[i]) + " ".repeat((max - stars[i] + 1) / 2));
    }
}

到目前为止你试过什么?看起来你至少需要两个来做循环这可能是家庭作业。你自己试过什么了吗?请注意,Stack Overflow不是免费的代码编写/家庭作业服务。因为我无法对您的问题添加注释,所以我必须在回答中发布它。非常感谢@MWB,我从未见过这种方法,我还没有发布我的实际工作,因为这对我来说变得非常棘手和复杂,因此,为了避免混淆,我刚刚添加了println语句。很高兴我能提供帮助!
import java.util.Arrays;
class Star {
    public static void main(String[] args) {
        int[] stars = new int[] { 1, 3, 9, 7, 9, 3, 1 }; // asterisks per line
        int max = Arrays.stream(stars).max().getAsInt(); // max asterisks in any line
        for (int i = 0; i < stars.length; i++) { // prints the asterisks for a given row, and pads it left and right with spaces
            System.out.print(" ".repeat((max - stars[i]) / 2) + "*".repeat(stars[i]) + " ".repeat((max - stars[i] + 1) / 2));
    }
}