java asterisk right triangle

前端 未结 5 1231
星月不相逢
星月不相逢 2020-12-11 11:56

I\'d like to try a right triangle asterisk. But I only got this output: (I can\'t place here the asterisk)

@ 
@@
@@@

what

5条回答
  •  Happy的楠姐
    2020-12-11 12:34

    Try This :

    public class PrintPyramidStar {  
    
       public static void main(String args[]) {  
    
        int c = 1;  
        for (int i = 1; i <= 5; i++) {  
            for (int j = i; j < 5; j++) {  
                System.out.print(" ");
            }  
            for (int k = 1; k <= c; k++) {  
                if (k % 2 == 0)  
                    System.out.print(" ");  
                else  
                    System.out.print("*");  
            }  
            System.out.println();  
            c += 2;  
        }  
      }  
    }  
    

提交回复
热议问题