打印数字反向金字塔

时间:2017-01-04 09:52:06

标签: android

我可以像这样打印金字塔:

   1
  123
 12345
1234567

用于打印上述数字金字塔的代码是:

                int a = 1;
                int b = 4;
                for (int i = 1 ; i <= 4 ; i++){
                    for (int c = 1 ; c <= b - 1 ; c++){
                        text.append("  ");
                    }
                    for (int k = 1 ; k <= a ; k++){
                        String result = String.valueOf(k);
                        text.append(result);
                    }
                    a = a + 2;
                    b--;
                    text.append("\n");
                }

但是,我面临的问题是我必须打印相同的金字塔,但顺序相反:

 1234567
  12345
   123
    1

任何帮助都会受到赞赏吗?

5 个答案:

答案 0 :(得分:1)

试试这个......

 for (int i = 7; i > 0; i--) {
        for (int j = 1; j <= i; j++) {
            System.out.print(j);
        }
        System.out.print("\n");

        if (i % 2 != 0) {
            i = i - 1;
        }
    }

答案 1 :(得分:0)

试试这个

SELECT id, replace(replace(category, '{', ''), '}', '') FROM table1

答案 2 :(得分:0)

试试这个:)

 int a = 1;
            int b = 4;
            int m = 4;

            for (int i = 1; i <= 4; i++) {
                for (int c = 1; c <= m; c++) {
                    text.append("  ");
                }
                for (int k = 1; k <= b; k++) {
                    String result = String.valueOf(k);
                    text.append(result);
                }
                a = a + 2;
                b--;
                m++;
                text.append("\n");
            }

答案 3 :(得分:0)

int w = 7;
        System.out.println("Here is your pattern....!!!");

        for (int i = 1; i <= 5; i++)
        {
            //Printing i spaces at the beginning of each row

            for (int j = 1; j < i; j++)
            {
                System.out.print(" ");
            }

            //Printing i to rows value at the end of each row

            for (int j = 1; j <=w; j++)
            {
                System.out.print(j+" ");
            }
            w = w - 2;

            System.out.println();
        }

答案 4 :(得分:0)

        int numberofdigits = 7;
        int numberofdigitsforrow = 0;
        int Emptyspace = 0;

        for (int i = 0; i < numberofdigits; i++)
        {
            numberofdigitsforrow = numberofdigits - i * 2;
            if (numberofdigitsforrow > 0)
            {
                Emptyspace = numberofdigits-numberofdigitsforrow;
                if (Emptyspace > 0)
                {
                    for (int b = 1; b <= Emptyspace/2; b++)
                    {
                        Console.Write(" ");

                    }
                }
                for (int c = 1; c <= numberofdigitsforrow; c++)
                {
                    Console.Write(c.ToString());
                }
                if (Emptyspace > 0)
                {
                    for (int b = 1; b <= Emptyspace / 2; b++)
                    {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }
        }