Printing *s as triangles in Java?

后端 未结 21 1863
囚心锁ツ
囚心锁ツ 2020-11-30 11:41

My assignment in my Java course is to make 3 triangles. One left aligned, one right aligned, and one centered. I have to make a menu for what type of triangle and then input

21条回答
  •  -上瘾入骨i
    2020-11-30 12:33

    This will print stars in triangle:

    `   
    public class printstar{
    public static void main (String args[]){
    int m = 0;
    for(int i=1;i<=4;i++){
    for(int j=1;j<=4-i;j++){
    System.out.print("");}
    
    for (int n=0;n<=i+m;n++){
    if (n%2==0){
    System.out.print("*");}
    else {System.out.print(" ");}
    }
    m = m+1;
    System.out.println("");
    }
    }
    }'
    

    Reading and understanding this should help you with designing the logic next time..

提交回复
热议问题