Java中如何创建半满半空二维数组并添加新行填充?
问题:实现半填充棋盘的逐步补全功能
我需要实现一个6列10行的棋盘,元素为随机数1、2、3。初始仅填充前5行,剩余5行留空。用户每次输入后,添加一行随机数替换空行,直到填满10行。
初始棋盘示例
3 1 1 2 2 2 3 3 3 3 2 3 1 1 1 3 3 2 2 3 2 3 1 2 3 3 1 2 2 2 What column do you want to shoot at?
用户输入后的棋盘示例
3 1 1 2 2 2 3 3 3 3 2 3 1 1 1 3 3 2 2 3 2 3 1 2 3 3 1 2 2 2 2 3 3 1 1 1 What column do you want to shoot at?
核心问题
- 如何创建半满半空的二维数组
- 如何在用户每次输入后,逐步用随机数行填充空行
现有代码
import java.util.Scanner; public class Project { public static void main( String[] args) { Scanner scan = new Scanner(System.in); int [][] grid = createBoard(6, /*Number of colums*/ 5, /*Number of rows*/ 3 /*Bubble types in the board (1, 2 or 3)*/); printGrid( grid ); System.out.println("What column do you want to shoot at?"); shoot(); } public static int[][] createBoard(int cols, int rows, int numBubbleTypes) { int[][] board = new int[rows][cols]; for( int row = 0; row < rows; row++ ) { for( int col = 0; col < cols; col++ ) { board[row][col] = (int)(Math.random() * (double)numBubbleTypes) + 1; } } return board; } public static void printGrid(int[][] grid) { for( int row = 0; row < grid.length; row++ ) { for( int col = 0; col < grid[row].length; col++ ) { System.out.print( grid[row][col] + " " ); } System.out.println(""); } } public static void fillBoard(int[] grid) { } public static void shoot() { Scanner scanShoot = new Scanner(System.in); String input = scanShoot.nextLine(); } private static void clearConsole() { System.out.print('\u000C'); //Clear terminal } }
解决方案
1. 创建半填充的二维数组
原createBoard只生成指定行数的数组,现在改成生成总10行的数组,仅填充前5行,剩余行用默认值0标记为空。
修改后的createBoard方法:
public static int[][] createBoard(int cols, int totalRows, int filledRows, int numBubbleTypes) { int[][] board = new int[totalRows][cols]; // 初始化总行数的数组 // 填充前filledRows行的随机数 for( int row = 0; row < filledRows; row++ ) { for( int col = 0; col < cols; col++ ) { board[row][col] = (int)(Math.random() * numBubbleTypes) + 1; } } // 剩余行默认是0,代表空行 return board; }
在main方法中调用时,传入总行数10、初始填充行数5:
int [][] grid = createBoard(6, 10, 5, 3);
2. 修改打印方法,适配空行
原打印方法会输出空行的0,需要判断行是否全为0,是则打印空行,否则打印数字:
public static void printGrid(int[][] grid) { for( int row = 0; row < grid.length; row++ ) { boolean isEmptyRow = true; // 检查当前行是否全为0 for( int val : grid[row] ) { if(val != 0) { isEmptyRow = false; break; } } if(isEmptyRow) { System.out.println(); // 空行直接换行 } else { for( int val : grid[row] ) { System.out.print( val + " " ); } System.out.println(); } } }
3. 实现逐步填充逻辑
在main方法中维护一个nextEmptyRow变量,记录下一个要填充的行索引。循环接收用户输入,每次输入后填充对应行,直到填满10行:
public static void main( String[] args) { Scanner scan = new Scanner(System.in); int totalRows = 10; int filledRowsInitially = 5; int [][] grid = createBoard(6, totalRows, filledRowsInitially, 3); int nextEmptyRow = filledRowsInitially; // 初始下一个空行是第5行(索引从0开始) while(nextEmptyRow < totalRows) { printGrid( grid ); System.out.println("What column do you want to shoot at?"); String input = scan.nextLine(); // 接收用户输入,无需处理内容,仅触发填充 // 填充下一行 fillNextRow(grid, nextEmptyRow, 3); nextEmptyRow++; // 可选:开启清屏,注释掉则保留历史输出 // clearConsole(); } // 填满后打印最终棋盘 printGrid(grid); System.out.println("棋盘已填满!"); scan.close(); }
实现填充单行的方法:
public static void fillNextRow(int[][] grid, int rowIndex, int numBubbleTypes) { int cols = grid[rowIndex].length; for( int col = 0; col < cols; col++ ) { grid[rowIndex][col] = (int)(Math.random() * numBubbleTypes) + 1; } }
4. 整合后的完整代码
import java.util.Scanner; public class Project { public static void main( String[] args) { Scanner scan = new Scanner(System.in); int totalRows = 10; int filledRowsInitially = 5; int [][] grid = createBoard(6, totalRows, filledRowsInitially, 3); int nextEmptyRow = filledRowsInitially; while(nextEmptyRow < totalRows) { printGrid( grid ); System.out.println("What column do you want to shoot at?"); String input = scan.nextLine(); fillNextRow(grid, nextEmptyRow, 3); nextEmptyRow++; // clearConsole(); // 可选开启清屏 } printGrid(grid); System.out.println("棋盘已填满!"); scan.close(); } public static int[][] createBoard(int cols, int totalRows, int filledRows, int numBubbleTypes) { int[][] board = new int[totalRows][cols]; for( int row = 0; row < filledRows; row++ ) { for( int col = 0; col < cols; col++ ) { board[row][col] = (int)(Math.random() * numBubbleTypes) + 1; } } return board; } public static void printGrid(int[][] grid) { for( int row = 0; row < grid.length; row++ ) { boolean isEmptyRow = true; for( int val : grid[row] ) { if(val != 0) { isEmptyRow = false; break; } } if(isEmptyRow) { System.out.println(); } else { for( int val : grid[row] ) { System.out.print( val + " " ); } System.out.println(); } } } public static void fillNextRow(int[][] grid, int rowIndex, int numBubbleTypes) { int cols = grid[rowIndex].length; for( int col = 0; col < cols; col++ ) { grid[rowIndex][col] = (int)(Math.random() * numBubbleTypes) + 1; } } private static void clearConsole() { System.out.print('\u000C'); // 清屏 } }
内容的提问来源于stack exchange,提问作者MazaPan616
相关产品推荐
相关产品推荐

