Make a upside down triangle in java

前端 未结 3 2068
感情败类
感情败类 2020-12-04 04:26

I am trying to make the triangle I have made up side down. Tried many times, but I don\'t know how to do this.

The code I have know is:

public stati         


        
3条回答
  •  独厮守ぢ
    2020-12-04 05:02

    import java.util.Scanner;
    
    public class EquilateralTraingle {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int side = sc.nextInt();
            constructEquTri(side);
    
        }
    
        private static void constructEquTri(int length) {
            // loop for each line
            for (int i = length; i > 0; i--) {
                // loop for initial spaces in each line
                for (int k = 0; k < length - i; k++) {
                    System.out.print(" ");
                }
                // loop for printing * in each line
                for (int j = 0; j < i; j++) {
    
                    System.out.print("*");
                    System.out.print(" ");
    
                }
                System.out.println();
            }
        }
    
    }
    /*Output:
        10
        * * * * * * * * * * 
         * * * * * * * * * 
          * * * * * * * * 
           * * * * * * * 
            * * * * * * 
             * * * * * 
              * * * * 
               * * * 
                * * 
                 * 
    */
    

提交回复
热议问题