详解for循环的Java小程序

高洛峰
发布: 2017-03-27 11:54:31
原创
2400人浏览过

1、
/*
* Created on 2004-8-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author diyer6
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/* 
此图是运行结果: 
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *
*/
public class XingHaoOne {
public static void main(String[] args) {
 for(int i=10;i>0;i--){
  for (int j=0; j < 10-i; j++) {

         System.out.print(" ");
        }

     for(int o=i;o>0;o--){
       System.out.print("*");
     }
  System.out.print("\n");
    }
  }

2、
/*
* Created on 2004-8-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author diyer6
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/*
此图是打印结果: 

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

*/
public class XingHaoTwo {

public static void main(String[] args) {
 for(int i=10;i>0;i--){ 
     for(int n=i;n<11;n++){ 

       System.out.print("*");
      }
  System.out.print("\n");
    }
  }
}

3、
/*
* Created on 2004-8-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author diyer6
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/*
此图是运行结果:

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

*/
public class XingHaoThree {

public static void main(String[] args) {
 for(int i=10;i>0;i--)
 {
  for(int j=0;j  System.out.print("*");
 System.out.print("\n");
 }
}
}

4、
/*
* Created on 2004-8-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author diyer6
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/*
此图是打印结果:

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

*/
public class XingHaoFour {

public static void main(String[] args) {
 for(int i=10;i>0;i--)
 {
  for(int j=i;j>1;j--){
    System.out.print(" ");}
  
  for(int n=i;n<11;n++){
       System.out.print("*");
     }
  
 System.out.print("\n");
 }

}

}

5、
/*
* Created on 2004-8-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author diyer6
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/*
此图是运行结果:

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

*/
public class XingHaoJuXing {

public static void main(String[] args) {
 
 for(int i=10;i>0;i--){
  
  for(int n=0;n  System.out.print("*");
  }
  
  for(int j=i;j<11;j++){
  System.out.print("*");
  }
  System.out.print("\n");
  
 }
}
}

6、
/*
* Created on 2004-8-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author diyer6
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/*
此图是打印结果: 

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

*/
public class Shishi {

public static void main(String[] args) {
 for(int i=10;i>0;i--){
  for(int f=0;f<10;f++){ 
  System.out.print("*");}

  System.out.print("\n");

   }
}
}

再来个带 注释 的 :

/*
* Created on 2004-8-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author diyer6
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/*
此图是打印结果:

0**********
00*********
000********
0000*******
00000******
000000*****
0000000****
00000000***
000000000**
0000000000*

*/
public class Zaishi {

public static void main(String[] args) {
 for(int i=10;i>0;i--){
  for(int j=i;j<11;j++){ //j=10;j<11;循环一次,打印一个 0 ,在执行 j++ ,
                  //此时j=11;i<11;为false,所以在第一行只打印了一个 0 。
  System.out.print("0");}
  for(int n=i;n>0;n--){ /*n=10;n>0;循环一次,打印一个 * ,在执行n--,
             *此时n=9;n>0;再循环一次,再打印一个 * ,在执行n--,
             *此时n=8;n>0;再循环一次,再打印一个 * ,在执行n--,
             *依次类推... 所就在第一行打印了10个 * 。
             *接下来执行 System.out.print("\n"); 换行 。
             *然后执行第一个大循环中的 i-- ; 此时 i 就等于 9 了。
             *
             *这时 第一个小循环 中:(j=i 从 i=9 开始循环)
             *j=9;j<11;循环两次,所以在第二行只打印 2 个 0 。
             *这时 第二个小循环 中:(n=i 从 i=9 开始循环)
             *n=9;n>0;循环一次,打印一个 * ,在执行n--,
             *此时n=8;n>0;再循环一次,再打印一个 * ,在执行n--,
             *依次类推... 所就在第二行打印了 9 个 * 。
             *接下来执行 System.out.print("\n"); 换行 。
             *然后执行第一个大循环中的 i-- ; 此时 i 就等于 8 了。
             *
             *往下 依次类推....
           */
  System.out.print("*");
  }
 
 System.out.print("\n");
 }
}
}
登录后复制

以上就是详解for循环的Java小程序的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号