A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王贵朝 中级黑马   /  2012-9-23 12:05  /  1610 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王贵朝 于 2012-9-23 14:27 编辑

需要打印出如下图形(用ForFor循环打印)

    *
  ***
*****

6 个回复

倒序浏览
本帖最后由 严海荣 于 2012-9-23 12:29 编辑
  1. public class Demo4 {
  2.         public static void main(String[] args) {
  3.                 Demo4 d = new Demo4();
  4.                 d.show4();
  5.         }

  6.         private void show4() {
  7.                 for (int a = 0; a < 5; a++) {
  8.                         for (int b = 1; b < 5 - a; b++) {
  9.                                 System.out.print(" ");

  10.                         }
  11.                         for (int c = 0; c <= a; c++) {
  12.                                 System.out.print("*" + " ");
  13.                         }

  14.                         System.out.println();
  15.                 }
  16.         }
  17. }这个是错的。。和题目不同,下面的是对的
复制代码
回复 使用道具 举报

  1. public class PrintDemo {
  2.         public static void main(String[] args) {
  3.                 int row = 3;
  4.                 for(int i=1;i<=row;++i){
  5.                         //打印空格
  6.                         for(int space=row-i;space>0;--space){
  7.                                 System.out.print(" ");
  8.                         }
  9.                        
  10.                         //打印*
  11.                         for(int j=1;j<=2*i-1;++j){
  12.                                 System.out.print("*");
  13.                         }
  14.                         // 换行
  15.                         System.out.println();
  16.                 }
  17.                
  18.         }
  19. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 赞一个!

查看全部评分

回复 使用道具 举报
  1. public class Demo4_1 {
  2.         public static void main(String[] args) {
  3.                 Demo4_1 d = new Demo4_1();
  4.                 d.show4_1(3);
  5.         }

  6.         private void show4_1(int num) {
  7.                 for (int a = 0; a < num; a++) {
  8.                         for (int b = 1; b < num - a; b++) {
  9.                                 System.out.print(" ");

  10.                         }
  11.                         for (int c = 0; c <= 2*a; c++) {
  12.                                 System.out.print("*");
  13.                         }

  14.                         System.out.println();
  15.                 }
  16.         }
  17. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

回复 使用道具 举报
本帖最后由 李建强 于 2012-9-23 13:14 编辑
  1. /*
  2. 需要打印出如下图形(用ForFor循环打印)
  3.            i         空格   *
  4.     *    0        2       1
  5.   ***  1         1       3
  6. *****2         0       5
  7. 一、外层i控制行数,内循环第一个控制空格,第二个控制*.
  8. 二、i和空格的关系:第一行空格=2-i,共打印line行,也就是空格=line-i-1。
  9. 三、i和*的关系:*数=i*2+1
  10. 四、
  11. 内循环的空格限制条件就是:j<line-i-1,j++
  12. 内循环的*限制条件就是:     j<i*2+1,j++
  13. */
  14. class TrianglePrint
  15. {
  16. public static void main(String [] args)
  17. {
  18.   print(10);
  19. }
  20. static void print(int line)
  21. {
  22.   for(int i=0;i<line;i++)
  23.   {
  24.    for(int j=0;j<line-i-1;j++)
  25.    {
  26.     System.out.print(" ");   
  27.    }
  28.      for(int j=0;j<2*i+1;j++)
  29.    {
  30.     System.out.print("*");   
  31.    }
  32.     System.out.println();   
  33.   }
  34. }
  35. }
复制代码
我的tab缩进是8,这个看着别扭了点。

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 赞一个!

查看全部评分

回复 使用道具 举报
  1. class Test5
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 for(int i=1;i<10;i+=2)
  6.                 {
  7.                         for(int a=(9-i)/2;a>0;a--)
  8.                         {
  9.                                 System.out.print(" ");
  10.                         }
  11.                         for(int j=0;j<i;j++)
  12.                         {
  13.                                 System.out.print("*");
  14.                         }
  15.                         System.out.println();
  16.                 }
  17.         }
  18. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 赞一个!

查看全部评分

回复 使用道具 举报
程振 发表于 2012-9-23 12:23

可以打印出来,自己想了1个小时也没想清楚。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马