如何使用Java生成星形三角形模式

如何使用Java生成星形三角形模式,java,Java,如何使用java打印星形三角形图案。模式是这样的 * ====> row 1 * * * * * * * * * * * 它可以是n行,从第二行开始,两颗星之间有奇数个空格,如1、3、5,最后一行的所有星星都被一个空格隔开 下面是我用来打印三角形的代码 public class Triangle { public static void main(String[] args)

如何使用java打印星形三角形图案。模式是这样的

            * ====> row 1
           * *
          *   *
         *     *
        * * * * *
它可以是n行,从第二行开始,两颗星之间有奇数个空格,如1、3、5,最后一行的所有星星都被一个空格隔开

下面是我用来打印三角形的代码

public class Triangle
 {
public static void main(String[] args)
{
    int row = 4;
    int space =0;
    System.out.println("*");
    for (int i=1;i<row;i++)
    {
        System.out.print("*");
        for(space=0;space<i;space = space+i)
        {
            System.out.print(" ");
        }
        System.out.print("*");
    System.out.println();
    }


enter code here
    for(int i=0;i<=4;i++)
    {
        System.out.print("* ");
    }
}
公共类三角形
{
公共静态void main(字符串[]args)
{
int行=4;
int空间=0;
System.out.println(“*”);
对于(int i=1;i
公共类三角形{
带STAR的公共静态无效绘图(整数维)
{
if(尺寸<0)
{
//假设尺寸=0的三角形是一个点。。。。
System.out.println(“无有效维度”);
}
其他的
{
//要打印第一个维度-1行
对于(int i=0;i
苹果包装;
公共阶级三角
{
公共静态void main(字符串…字符串){
int midspace=-1;
int行=4;
弦星=”;
对于(int y=row-1;y>0;y--){
对于(int-space=1;空格0;i--)
系统输出打印(“”);
中间空间+=2;
星形=(y!=第1行)?“*”:”;
System.out.println(星型);
}
对于(int y=((第*2行)-1);y>0;y--){
系统输出打印(“*”);
}
}
}

如果您向我们展示您是如何开始的,我们可以告诉您如何继续。我能够通过使用堆栈溢出的帮助来实现以下目标,公共类三角形{public static void main(String[]args){for(int i=1;i到目前为止您尝试了什么?您注意到有一个模式可以遵循,那么确切的问题在哪里。我建议阅读。显示您用于打印模式的代码基本上我可以打印完整的三角形
public class Triangle {

    public static void DrawWithStars(int dimension)
    {
        if(dimension < 0)
        {
            //Assuming that a triangle with dimension = 0 is a dot....
            System.out.println("No valid dimension");
        }
        else
        {
            //To print the first dimension - 1 rows
            for (int i = 0; i < dimension; i++) 
            {
                for (int j = 0; j < dimension - i; j++) {
                    System.out.print(" ");
                }

                //Print the dot of the row 1 at the end
                if(i != 0)
                    System.out.print("*");

                for (int j = 0; j < 2 * i - 1; j++) {
                    System.out.print(" ");
                }

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

            //To print the last row

            for (int i = 0; i < dimension; i++)
            {
                System.out.print("* ");
            }

            System.out.println("*");
        }
    }
}
package apple;

public class Triangle
{
public static void main(String...strings){
    int midspace = -1;
    int row = 4;
    String star = "";
    for(int y=row-1; y>0; y--){ 
        for(int space = 1;space <= y ; space++){
            System.out.print(" ");
        }
        System.out.print("*");

        for(int i = midspace; i>0; i--)
            System.out.print(" ");
        midspace += 2;

        star = (y!=row-1) ? "*":"";
        System.out.println(star);

    }


    for(int y=((row*2)-1); y>0; y--){
        System.out.print("*");
    }
}

}