当前位置: 首页 > 图灵资讯 > 技术篇> 用于循环编号模式:

用于循环编号模式:

来源:图灵教育
时间:2025-02-18 20:52:57

>数字模式(1to9):

package b1;

public class npattern {

    public static void main(string[] args) {
        // npattern1();
        // npatern2();
        // npattern3();
        // npatern4();
        // npatern5();
        // npatern6();
        // npattern7();
        // npatern8();
        // npattern9();
        npatern10();

    }

    private static void npatern10() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (col == 3 || col == 9 || col == 1)
                    system.out.print("* ");
                else if (row == 1 && col >= 3 || row == 9 && col >= 3)
                    system.out.print("* ");
                else
                    system.out.print("  ");
            }
            system.out.println();
        }

    }
output:

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


    private static void npattern9() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 9 || row == 1 || col == 9)
                    system.out.print("* ");
                else if (row == 5 || col == 1 && row <= 5)
                    system.out.print("* ");
                else
                    system.out.print("  ");
            }
            system.out.println();
        }

    }
output:

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



    private static void npatern8() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 5 || row == 1 || row == 9)
                    system.out.print("* ");
                else if (col == 1 || col == 9)
                    system.out.print("* ");
                else
                    system.out.print("  ");
            }
            system.out.println();
        }
    }
output:

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


    private static void npattern7() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 1 || row + col == 10)
                    system.out.print("* ");

                else
                    system.out.print("  ");
            }
            system.out.println();
        }

    }
output:

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

    private static void npatern6() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 5 || row == 1 || row == 9)
                    system.out.print("* ");
                else if (col == 1 || col == 9 && row >= 5)
                    system.out.print("* ");
                else
                    system.out.print("  ");
            }
            system.out.println();
        }
    }
output:

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


    private static void npatern5() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 5 || row == 1 || row == 9)
                    system.out.print("* ");
                else if (col == 1 && row <= 5 || col == 9 && row >= 5)
                    system.out.print("* ");
                else
                    system.out.print("  ");
            }
            system.out.println();
        }

    }
output:

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


    private static void npatern4() {

        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 5 || col == 5)
                    system.out.print("* ");
                else if (row + col == 6 && row <= 5)
                    system.out.print("* ");
                else
                    system.out.print("  ");
            }
            system.out.println();
        }
    }
output:

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

    private static void npattern3() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 9 || row == 5 || row == 1)
                    system.out.print("* ");
                else if (col == 9 && row <= 5 || col == 9 && row >= 5)
                    system.out.print("* ");
                else
                    system.out.print("  ");
            }
            system.out.println();
        }

    }
output:

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


    private static void npatern2() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 9 || row == 5 || row == 1)
                    system.out.print("* ");
                else if (col == 1 && row >= 5 || col == 9 && row <= 5)
                    system.out.print("* ");
                else
                    system.out.print("  ");
            }
            system.out.println();
        }
    }
output:

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


    private static void npattern1() {
        for (int row = 1; row <= 9; row++) {
            for (int col = 1; col <= 9; col++) {
                if (row == 9 || col == 5 || col + row == 6 && row <= 4)
                    system.out.print("* ");
                else
                    system.out.print("  ");
            }
            system.out.println();
        }
    }
}

output:

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


星数和数字模式:>

package B1;

public class NSpattern {

    public static void main(String[] args) {
        pattern();
        // pattern1();
        // pattern2();
        // pattern3();
        // pattern4();
        // pattern5();
        // pattern6();

    }

    private static void pattern() {
        for (int row = 1; row <= 5; row++) {
            for (int col = 1; col <= row; col++)
                System.out.print("* ");

            System.out.println();
        }

    }
output:
* 
* * 
* * * 
* * * * 
* * * * * 


    private static void pattern6() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(row + col + " ");
            System.out.println();
        }

    }
output:

6 7 8 9 10 
5 6 7 8 
4 5 6 
3 4 
2 


    private static void pattern5() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(row / col + " ");
            System.out.println();
        }
    }
output:

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


    private static void pattern4() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(row * col + " ");
            System.out.println();
        }
    }
output:

5 10 15 20 25 
4 8 12 16 
3 6 9 
2 4 
1



    private static void pattern3() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(col + " ");
            System.out.println();
        }
    }
output:

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



    private static void pattern2() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print(row + " ");
            System.out.println();
        }
    }
output:

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

    private static void pattern1() {
        for (int row = 5; row >= 1; row--) {
            for (int col = 1; col <= row; col++)
                System.out.print("* ");
            System.out.println();
        }

    }

}
output:

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

以上是循环编号模式:详情请关注图灵教育的其他相关文章!