按顺序显示一个数字递增的框

代码


        int rows=3, columns=3, i, j;

        for(i = 1; i <= rows; i++)

        {

            for(j = 1; j <= columns; j++)

            {

                if(i == 1 || i == rows || j == 1 || j == columns)

                {

                    System.out.print(count); 

                    count++;

                }

                else

                {

                    System.out.print(" "); 

                }

            }

            System.out.print("\n"); 


        }   

以下代码输出以下内容:


输出


123

4 5

678

我想要实现的是以下内容:


123

8 4

765

基本上创建一个棋盘游戏,它必须从一个位置开始并在同一位置结束,完成一个完整的圆圈,在这种情况下是一个正方形。


有任何想法吗 ??


拉丁的传说
浏览 136回答 2
2回答

九州编程

经过一些实验,这可以工作:public static void printBox(int rows, int columns) {&nbsp; &nbsp; int sumOfColumns = 2 * columns + rows - 1;&nbsp; &nbsp; int sumOfRow = 2 * columns + 2 * rows - 2;&nbsp; &nbsp; for (int i = 1; i <= rows; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 1; j <= columns; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf(String.format("%3d", j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (j == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf(String.format("%3d", sumOfRow - (i + j - 1)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (j == columns) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf(String.format("%3d", i + j - 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (i == rows) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf(String.format("%3d", sumOfColumns - j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("&nbsp; &nbsp;");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\n");&nbsp; &nbsp; }&nbsp; &nbsp; System.out.print("\n");}测试用例:public static void main(String[] args) {&nbsp; &nbsp; printBox(3, 3);&nbsp; &nbsp; printBox(4, 4);&nbsp; &nbsp; printBox(3, 4);}结果:&nbsp; 1&nbsp; 2&nbsp; 3&nbsp; 8&nbsp; &nbsp; &nbsp;4&nbsp; 7&nbsp; 6&nbsp; 5&nbsp; 1&nbsp; 2&nbsp; 3&nbsp; 4&nbsp;12&nbsp; &nbsp; &nbsp; &nbsp; 5&nbsp;11&nbsp; &nbsp; &nbsp; &nbsp; 6&nbsp;10&nbsp; 9&nbsp; 8&nbsp; 7&nbsp; 1&nbsp; 2&nbsp; 3&nbsp; 4&nbsp;10&nbsp; &nbsp; &nbsp; &nbsp; 5&nbsp; 9&nbsp; 8&nbsp; 7&nbsp; 6

慕侠2389804

考虑创建一个二维数组:gameBoard[3][3];然后用所需的值填充该板。游戏板完成后,您可以编写另一个函数来打印板。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java