登录
首页 >  文章 >  java教程

用于循环编号模式:

来源:dev.to

时间:2025-02-15 18:09:50 275浏览 收藏

文章不知道大家是否熟悉?今天我将给大家介绍《用于循环编号模式:》,这篇文章主要会讲到等等知识点,如果你在看完本篇文章后,有更好的建议或者发现哪里有问题,希望大家都能积极评论指出,谢谢!希望我们能一起加油进步!

>数字模式(1to9):

package b1;

public class npattern {

    public static void main(string[] args) {
        // npattern1();
        // npattern2();
        // npattern3();
        // npatern4();
        // npattern5();
        // npattern6();
        // npattern7();
        // npattern8();
        // npattern9();
        npattern10();

    }

    private static void npattern10() {
        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 npattern8() {
        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 npattern6() {
        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 npattern5() {
        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 npattern2() {
        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:

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

本篇关于《用于循环编号模式:》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>