Java打印直角三角形、等腰三角形、菱形、沙漏形


public class Test_s {    

     public void s1() {//正直角三角形

         for (int i = 1; i <= 5; i++) {

              for (int j = 1; j <= i; j++) {

                   System.out.print("*");

              }System.out.println();

         }

     }

    

     public void s2() {//倒直角三角形

         for (int i = 1; i <=5; i++) {

              for (int j = 1; j <= 6 - i; j++) {

                   System.out.print("*");

              }System.out.println();

         }

     }

    

     public void s3() {//正直角三角形水平翻转

         for (int i = 1; i <=5; i++) {

              for (int j = 1; j <= 5; j++) {

                   System.out.print(j > 5 - i ? "*":" ");

              }System.out.println();

         }

     }

    

     public void s4() {//倒正直角三角形水平翻转

         for (int i = 1; i <=5; i++) {

              for (int j = 1; j <= 5; j++) {

                   System.out.print(j >= i ? "*":" ");

              }System.out.println();

         }

     }

    

     public void s5() {//等腰三角形

         int len = 7;

         for (int i = 1; i <= 4; i++) {

              for (int j = 1; j <= len; j++) {

                   System.out.print(j > len/2-i+1 && j <= len/2+i ? "*":" ");

              }System.out.println();

         }

     }

    

     public void s6() {//等腰三角形垂直翻转

         int len = 13;

         for (int i = 1; i <= 4; i++) {

              for (int j = 1; j <= len; j++) {

                   System.out.print(j > (i - 1) * 2 && j < len + 3 - 2 * i ? "*" : " ");

              }System.out.println();

         }

     }

 

     public void s7() {//菱形

         int height = 7;

         for (int i = 1; i <= height; i++) {

              for (int j = 1; j <= height; j++) {

                   if (i <= height / 2 + 1) {

                       System.out.print(j > height/2-i+1 && j <= height/2+i ? "*":" ");

                   } else {

                       System.out.print(j > i - height / 2 - 1 && j < height - i + height / 2 + 2 ? "*" : " ");

                   }

              }

              System.out.println();

         }

     }

    

     public void s8() {//沙漏形

         int height = 7;

         for (int i = 1; i <= height; i++) {

              for (int j = 1; j <= height; j++) {

                   if (i <= height / 2 + 1) {

                       System.out.print(j > i - 1 && j < height + 2 - i ? "*" : " ");

                   } else {

                       System.out.print(j > height-i && j < 1+i ? "*":" ");

                   }

              }

              System.out.println();

         }

 

     }

     public static void main(String[] args) {

         // TODO Auto-generated method stub

         Test_s t_s = new Test_s();

         t_s.s1();

         System.out.println("-----------");

         t_s.s2();

         System.out.println("-----------");

         t_s.s3();

         System.out.println("-----------");

         t_s.s4();

         System.out.println("-----------");

         t_s.s5();

         System.out.println("-----------");

         t_s.s6();

         System.out.println("-----------");

         t_s.s7();

         System.out.println("-----------");

         t_s.s8();       

     }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM