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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shuaiqi_P_D 中级黑马   /  2015-6-9 23:28  /  375 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //12345678987654321



  2. //123456787654321



  3. // 1234567654321



  4. //  12345654321



  5. //   123454321



  6. //    1234321



  7. //     12321



  8. //      121



  9. //       1
复制代码


5 个回复

倒序浏览
是要写程序输出这样的么
  1. package com.itheima;

  2. public class Paint
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 draw(9);
  7.         }

  8.         private static void draw(int i) {
  9.                 // TODO 自动生成的方法存根
  10.                 for (int x = i; x>0; x--)
  11.                 {
  12.                         for (int y = 0; y<i-x; y++)
  13.                                 System.out.print(" ");
  14.                         for(int z = 1; z<=x; z++)
  15.                         {
  16.                                 System.out.print(z);
  17.                         }
  18.                         for(int z = x-1; z>=1; z--)
  19.                         {
  20.                                 System.out.print(z);
  21.                         }
  22.                         System.out.println();
  23.                        
  24.                 }
  25.                        
  26.         }
  27. }
复制代码
回复 使用道具 举报
我写的另外一种思路:;P

package com.itheima;

public class Demo {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                new Demo().printDemo(9);

        }

        public void printDemo(int n) {

                String str = " ";

                StringBuilder sb = new StringBuilder();
                int i = n;
                while (i > 0) {

                        int j = 0;

                        while (j < i) {
                                j++;
                                sb.insert(0, j);
                        }
                        j--;
                        while (j > 0) {
                                sb.insert(0, j);
                                j--;
                        }
                        while (j < n - i) {
                                sb.insert(0, str);
                                j++;
                        }
                        System.out.println(sb.toString());
                        sb.delete(0, sb.length());
                        i--;
                }
        }

}
回复 使用道具 举报
路过学习下
回复 使用道具 举报
  1. public class Demo {

  2.         public static void main(String[] args) {
  3.                 // TODO Auto-generated method stub
  4.                 printDemo(9);
  5.         }

  6.         public static void printDemo(int n) {
  7.                 while (n >= 1) {// 控制打印多少行数据
  8.                         StringBuilder sb = new StringBuilder();
  9.                         for (int i = 1; i <= n; i++) {// 负责存入从小到大的数据,如:123456789
  10.                                 sb.append(i);
  11.                         }
  12.                         for (int i = n - 1; i >= 1; i--) {// 用于存入从大到小的数据,如:87654321。存完后sb中的内容为12345678987654321
  13.                                 sb.append(i);
  14.                         }
  15.                         for (int i = 0; i < 9 - n; i++) {// 在第一列的位置插入空格。
  16.                                 sb.insert(0, ' ');
  17.                         }
  18.                         System.out.println(sb.toString());
  19.                         n--;
  20.                 }
  21.         }
  22. }
复制代码
回复 使用道具 举报
思路学习了,自己敲。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马