Printing *s as triangles in Java?

后端 未结 21 1864
囚心锁ツ
囚心锁ツ 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 12:39

    You might be interested in this too

          Scanner sc = new Scanner(System.in);
          int n=sc.nextInt();
          int b=0;
          for(int i=n;i>=1;i--){
          if(i!=n){
          for(int k=1;k<=b;k++){
          System.out.print(" ");
            }
                }
           for(int j=i;j>=1;j--){
           System.out.print("*");
           if(i!=1){
           System.out.print(" ");
            }
                }
           System.out.println();
           b=b+2;
            }
    

    Output:: 5

           * * * * * 
             * * * * 
               * * * 
                 * * 
                   *
    

提交回复
热议问题