zoukankan      html  css  js  c++  java
  • 用循环打印出多种三角形

    打印一排*,很简单,打印下图

     

    也很简单,代码如下: 

     1 public class Work10_3 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         int a=0;
     9         while(a<4){
    10             int i=0;
    11             while(i<10){
    12                 System.out.print("*");//打印*不换行
    13                 i++;
    14             }
    15             System.out.println("");//换行
    16             a++;
    17         }
    18     }
    19 }

    可是昨天想了好久都没想到怎样做到下面图片的样子,今天突然就有了灵感

    代码很简单,就是昨天想破了脑袋都想不出来,好笨啊我

    第一行打印一个*,第二行行打印两个*,大三行打印三个*,这样分析就找到规律了,定义一个a=1,外层循环实现打印几行,定义一个i=0,

    实现内层循环打印*,当a=1时是第一行,想让内层打印一个*,那么内层循环条件是i<1,这样就打印一个*,当a=2时,是第二行,想让内

    层打印两个*,那么内层就是i<2,这样又不难看出i<a,于是代码如下:

     1 public class Work10 {
     2     /**
     3      * @param args
     4      */
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7         int a=1;
     8         while(a<25){
     9             int i=0;
    10             while(i<a){
    11                 System.out.print("*");//打印*不换行
    12                 i++;
    13             }
    14             System.out.println("");//换行
    15             a++;
    16         }
    17     }
    18 }

    很简单的代码,还可以改进一下。

    有了一个灵感之后,就不能浪费,要充分锻炼自己的才能,

    于是我又打印了一条斜线

    这是往右斜着的

    让内层打印空格(和上面内层打印*一样),外层打印一个*,和刚才外层有些微小区别

    代码如下:

     1 public class Work10_1 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         int a=1;
     9         while(a<25){
    10             int i=0;
    11             while(i<a){
    12                 System.out.print(" ");//打印空格不换行
    13                 i++;
    14             }
    15             System.out.print("*
    ");//打印*后换行
    16             a++;
    17         }
    18     }
    19 
    20 }

     

    这是往左斜着的,内层我定义i=25,a=1时打印24个空格,然后打印*换行,当a=2时,打印23个空格,然后打印*换行。。。。。代码如下:

     1 public class Work10_2 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         int a=1;
     9         while(a<25){
    10             int i=25;
    11             while(i>a){
    12                 System.out.print(" ");//打印空格不换行
    13                 i--;
    14             }
    15             System.out.print("*
    ");//打印*后换行
    16             a++;
    17         }
    18     }
    19 
    20 }

    只有做不到的,没有想不到的,看下图

     

    和上面一样的方法,一个内层while打印空格,另一个打印*,

    这个也很简单,要是之前的我肯定不会这么说,现在知道怎么做了,就感觉简单了,

    代码如下:

     1 public class Work10_4 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         int a=1;
     9         while(a<25){
    10             int i=25;
    11             while(i>a){
    12                 System.out.print(" ");//打印空格不换行
    13                 i--;
    14             }
    15             int b=0;
    16             while(b<a){
    17                 System.out.print("*");//打印*不换行
    18                 b++;
    19             }
    20             System.out.println("");//换行
    21             a++;
    22         }
    23     }
    24 
    25 }

    收回刚才的话啊 下面这个我没想到,就是改了下代码就变这样了,其实我想做一个等腰三角形的

     

    代码如下:

     1 public class Work10_5 {
     2     public static void main(String[] args) {
     3         // TODO Auto-generated method stub
     4         int a=0;
     5         while(a<25){
     6             int i=25;
     7             while(i>a){
     8                 System.out.print(" ");//打印空格不换行
     9                 i--;
    10             }
    11             int b=0;
    12             while(b<a){
    13                 System.out.print("*");
    14                 b+=2;//这里和上面的不一样
    15             }
    16             System.out.println("");
    17             a++;
    18         }
    19     }
    20 }

    这个等腰三角形不好做啊 试了几次没有做出来,我要在定义一个变量试一试…

    原来真是需要添加一个变量,添加以后瞬间就做出来了

     

    分析一下,第一行先打印好多空格,然后一个*,第二行空格减少一个,*增加两个,因为增加的速度不一样,所以需要两个变量分别控制两个内层循环,空格的打印和之前的都一样, 只是打印*的速度要增加,代码如下:

     1         // TODO Auto-generated method stub
     2         int a=1;
     3         int c=1;
     4         while(a<25){
     5             int i=25;
     6             while(i>a){
     7                 System.out.print(" ");
     8                 i--;
     9             }
    10             int b=0;
    11             while(b<c){
    12                 System.out.print("*");
    13                 b++;
    14             }
    15             System.out.println("");
    16             a++;
    17             c+=2;
    18         }
    19     

    然后我又想打印一个倒三角,然后很轻松就打印出来了

     

    这个就不多说了,代码如下:

     1 public class Work10_6 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         int a=1;
     9         int c=1;
    10         while(a<25){
    11             int i=25;
    12             while(i>a){
    13                 System.out.print(" ");
    14                 i--;
    15             }
    16             int b=0;
    17             while(b<c){
    18                 System.out.print("*");
    19                 b++;
    20             }
    21             System.out.println("");
    22             a++;
    23             c+=2;
    24         }
    25     }
    26 
    27 }

     还能把这写三角形组合起来,有多种组合方式,下面提供一种做参考:

    代码如下:

     1 public class Work10_13 {
     2 
     3     /**
     4      * @param args
     5      */
     6     public static void main(String[] args) {
     7         // TODO Auto-generated method stub
     8         int a=1;
     9         int c=1;
    10         while(a<20){
    11             int i=20;
    12             while(i>a){
    13                 System.out.print(" ");
    14                 i--;
    15             }
    16             int b=0;
    17             while(b<c){
    18                 System.out.print("*");
    19                 b++;
    20             }
    21             System.out.println("");
    22             a++;
    23             c+=2;
    24         }
    25         int d=0;
    26         int e=39;
    27         while(d<20){
    28             int i=0;
    29             while(i<d){
    30                 System.out.print(" ");//打印空格不换行
    31                 i++;
    32             }
    33             int b=0;
    34             while(b<e){ 
    35                 System.out.print("*");
    36                 b++;
    37             }
    38             System.out.println("");
    39             d++;
    40             e-=2;
    41         }
    42     }
    43 }

    ------------------------------------------------------------------------------------------------------------------------------------------------------------

    转载请注明出处!

    面朝大海
  • 相关阅读:
    LwIP源代码文件目录解析
    规划2014!
    test
    111
    df
    第一次北京之行
    Android02--debug.keystore的注册信息
    Android01--开发环境搭建
    dfd
    1007
  • 原文地址:https://www.cnblogs.com/ln-ing/p/6165835.html
Copyright © 2011-2022 走看看