我的嵌套for循环数模式并没有输出正确的模式

时间:2017-04-01 21:33:47

标签: java

我试图打印出这种模式,但是我在排列数字并将它们分开以达到预期的输出时遇到了麻烦。

Expected Output:
          1
        2 1
      3 2 1
    4 3 2 1
  5 4 3 2 1
6 5 4 3 2 1

1 2 3 4 5 6
  1 2 3 4 5
    1 2 3 4
      1 2 3
        1 2
          1

这是我的代码。我不知道如何分配数字并按照输出指定的顺序输入正确的数字。

import java.util.Scanner;

public class patterns{
 public static void main(String[] args) 
{
    Scanner sc = new Scanner(System.in);

   for (int i = 6; i >= 1; i--) 
    {


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



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

        System.out.println();
    }


                System.out.println();


   for (int i = 1; i <= 6; i++) 
    {


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



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

        System.out.println(); 
    } 

   }
 }

这是输出I&#39;获取

run:
     6
    56
   456
  3456
 23456
123456

123456
 23456
  3456
   456
    56
     6

5 个答案:

答案 0 :(得分:0)

这里是您的模式的修改代码。

public class Patterns {
    public static void main(String[] args) {

        for (int i = 0; i < 6; i++) {

            for (int j = 0; j < (5 - i) * 2; j++) {
                System.out.print(" ");
            }

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

            System.out.println();
        }

        System.out.println();

        for (int i = 0; i < 6; i++) {

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

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

            System.out.println();
        }

    }
}

输出

          1 
        2 1 
      3 2 1 
    4 3 2 1 
  5 4 3 2 1 
6 5 4 3 2 1 

1 2 3 4 5 6 
  1 2 3 4 5 
    1 2 3 4 
      1 2 3 
        1 2 
          1 

更新:修复空格错误

工作=&GT;考虑模式的前半部分

          1 line 0: 10 spaces  = (5 - 0) * 2 spaces
        2 1 line 1: 8  spaces  = (5 - 1) * 2 spaces
      3 2 1 line 2: 6  spaces  = (5 - 2) * 2 spaces
    4 3 2 1 line 3: 4  spaces  = (5 - 3) * 2 spaces
  5 4 3 2 1 line 4: 2  spaces  = (5 - 4) * 2 spaces
6 5 4 3 2 1 line 5: 0  spaces  = (5 - 5) * 2 spaces

考虑模式的后半部分

1 2 3 4 5 6 line 0: 0 spaces  = 0 * 2 spaces
  1 2 3 4 5 line 1: 2 spaces  = 1 * 2 spaces
    1 2 3 4 line 2: 4 spaces  = 2 * 2 spaces
      1 2 3 line 3: 6 spaces  = 3 * 2 spaces
        1 2 line 4: 8 spaces  = 4 * 2 spaces
          1 line 5: 10 spaces = 5 * 2 spaces

答案 1 :(得分:0)

你应该认为每个三角形块实际上有6x6个方格单元。所以这意味着你需要2个嵌套循环,每个循环迭代6次。

然后在内部,您将决定是否打印数字或空格,具体取决于某些条件。

for (int i = 6; i >= 1; i--)
    {
        for (int j = 1; j <= 6; j++)
        {
            if (j < i)
            {
                System.out.print("  ");
            } else
            {
                System.out.print(7 - j + " ");
            }
        }

        System.out.println("");
    }

    System.out.println("");

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

        System.out.println("");
    }


          1 
        2 1 
      3 2 1 
    4 3 2 1 
  5 4 3 2 1 
6 5 4 3 2 1 

1 2 3 4 5 6 
  1 2 3 4 5 
    1 2 3 4 
      1 2 3 
        1 2 
          1 

答案 2 :(得分:0)

这样做的一种方法是:

import java.util.Scanner;

public class pattern{
 public static void main(String[] args) 
{
    Scanner sc = new Scanner(System.in);


   for (int i = 1; i <= 6; i++) 
    {

       String val = "";

        for (int j = i; j >= 1; j--)
        {
            val += j+ " ";

        }

        System.out.printf("%12s", val);
        System.out.println();
    }


                System.out.println();


   for (int i = 6; i >= 1; i--) 
{
   String val = "";

   int ii=1;
    for (int j = i; j >= 1; j--) 
    { 
        val +=   ii + " ";
        ii++;
    } 

    System.out.printf("%12s", val);
    System.out.println();
} 

   }

输出:

          1
        2 1
      3 2 1
    4 3 2 1
  5 4 3 2 1
6 5 4 3 2 1



1 2 3 4 5 6 
  1 2 3 4 5 
    1 2 3 4 
      1 2 3 
        1 2 
          1 

要记住的一件事是,当您尝试格式化文本时,应使用printf而不是添加打印行。

答案 3 :(得分:0)

    int a = 654321;
    String str = new Integer(a).toString();

    for (int i = str.length() - 1; i >= 0  ; i--) {
        String g = String.format("%6s", str.substring(i));
        System.out.println(g.replaceAll(".(?=.)", "$0 "));
    }

    System.out.println();

    str = new StringBuffer(str).reverse().toString();
    for (int i = str.length() - 1; i >= 0  ; i--) {
        String g = String.format("%6s", str.substring(0, i+1));
        System.out.println(g.replaceAll(".(?=.)", "$0 "));
    }

答案 4 :(得分:0)

最好的方法是使用数组 - &gt;你循环次数减少了。 http://ideone.com/diYtvM干杯!

  import java.util.Scanner;

        public class patterns{
         public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            int[][] arr = new int[6][6];

                for(int i = 0; i<6; i++)
                    arr[i][5] = 1;


    for(int i = 1; i<6; i++)
                for (int j = 4; j>=5-i; j--)
                {
                    arr[i][j] = arr[i-1][j+1] + 1; 
                }

            for(int i = 0; i<6; i++)
            {
                for (int j = 0; j<6; j++)
                {
                    if(arr[i][j] > 0)
                    System.out.print(arr[i][j] + " ");
                    else
                    System.out.print("  ");

                }
                    System.out.println("");
            }


            System.out.println("");

            int z = 0;
                for(int i = 5; i>=0; i--)
            {
                for(int k = 0; k<z; k++)
                System.out.print("  ");
                for (int j = 5; j>=0; j--)
                {
                    if(arr[i][j] > 0)
                    System.out.print(arr[i][j] + " ");

                }
                    System.out.println("");
                    z++;
            }

         }

        }

OUT:

          1 
        2 1 
      3 2 1 
    4 3 2 1 
  5 4 3 2 1 
6 5 4 3 2 1 

1 2 3 4 5 6 
  1 2 3 4 5 
    1 2 3 4 
      1 2 3 
        1 2 
          1