用java打印对角线三角形

用java打印对角线三角形,java,Java,我需要打印一个三角形,看起来像这样: * ** *** **** 我现在掌握的密码 for(line = 0; line < size; line++){ for(count = 0; count < line; count++){ System.out.print("*"); for(space = 0; space < line; space++) Syst

我需要打印一个三角形,看起来像这样:

*
 **
  ***
   ****
我现在掌握的密码

    for(line = 0; line < size; line++){
        for(count = 0; count < line; count++){
            System.out.print("*");
            for(space = 0; space < line; space++)
                System.out.print(" ");
        }
            System.out.println();
    }

您正在同一行上打印空格。调用
System.out.println()打印空格之前

编辑-示例:

for (line = 0; line < size; line++){
    for(space = 0; space < line - 1; space++)
        System.out.print(" ");
    for (count = 0; count < line; count++)
        System.out.print("*");
    System.out.println();
}
for(行=0;行
用于(line=0;line
您需要首先打印前缀空格。然后打印星星

试试这个:

    int line = 0;
    int size = 6;
    int count = 0;
    int space = 0;
    for (line = 0; line < size; line++) {
        //print spaces
        for (space = 0; space < line; space++)
            System.out.print(" ");

        //Print stars
        //Note: here count condition should be count < line+1, rather than count < line
        //If you do not do so, the first star with print as space only.
        for (count = 0; count < line+1; count++) {
            System.out.print("*");

        }
        System.out.println();
    }
类金字塔
{
公共静态void main(字符串参数[])
{
java.util.Scanner pyr=新的java.util.Scanner(System.in);
System.out.println(“键入用于制作金字塔的编号”);
int n=pyr.nextInt();
对于(int i=1;ii;j--)
{
系统输出打印(“”);
} 

对于(int k=1;k只需在星号之前打印空格就可以了。

暂时将空格替换为+或其他字符,这样会更清楚地看到发生了什么事情。您试图实现的模式到底是什么?我的意思是您展示了它,但我没有看到固定的模式。您能用文字描述一下吗?代码是u给我们的不是第二种模式,它更多地与算法相关,而不仅仅是java
for(line = 0; line < size; line++){
    for(space = 0; space < line; ++space)
        System.out.print(" ");
    for(count = 0; count < line; count++)
        System.out.print("*");
    System.out.println();
}
    int line = 0;
    int size = 6;
    int count = 0;
    int space = 0;
    for (line = 0; line < size; line++) {
        //print spaces
        for (space = 0; space < line; space++)
            System.out.print(" ");

        //Print stars
        //Note: here count condition should be count < line+1, rather than count < line
        //If you do not do so, the first star with print as space only.
        for (count = 0; count < line+1; count++) {
            System.out.print("*");

        }
        System.out.println();
    }
*
 **
  ***
   ****
    *****
     ******
  class Pyramid 
   {
    public static void main(String args[])
    {
       java.util.Scanner pyr=new java.util.Scanner(System.in);
       System.out.println("type a no. to make Pyramid");
       int n= pyr.nextInt();
       for (int i=1; i<=n; i++)
       {
           for(int j=n; j>i; j--)
           {
               System.out.print(" ");
           } 
           for(int k=1; k<=i; k++)
           {
              System.out.print("* ");
           }
            System.out.println(); 
        }
    }