Java数组生成问题:如何创建含5个不相邻"A"的29索引数组?
问题需求
编写Java程序,实现以下功能:
- 创建一个长度为29的
String数组 - 数组中包含且仅包含5个"A",其余24个元素为"-"
- 所有"A"之间不能相邻
- 按指定格式打印数组
当前实现代码存在两个问题:要么抛出ArrayIndexOutOfBoundsException异常,要么无法保证数组中恰好生成5个"A"。
用户原代码实现
static String[] placeRandomAppleAroundBoard(String[] board) throws java.lang.ArrayIndexOutOfBoundsException{ //ADD "A" TO RANDOM INDEX IN BOARD int x = 5; while (checkForAInArray(board)==false) { for (int i = 0; i < x; i++) { int j = (int)(Math.random()*board.length); while(board[j-1].equals("A") || board[j+1].equals("A")){ //Make sure there's no A beside another "A" j = (int)(Math.random()*board.length); } board[j] = "A"; } x = CountAInArray(board); } return board; } static boolean checkForAInArray(String[] board){ //Make sure there is 5 "A" in the program int countOfA = 0; for (int i = 0; i < board.length; i++) { if(board[i].equals("A")){ countOfA++; } } if(countOfA==5){ return true; } else{ return false; } } static int CountAInArray(String[] board){ //Control the number of time the For-loop of placeRandomAppleAroundBoard iterate, which is 5 based and then change depending on how much "A" there's on the board int countOfA = 0; for (int i = 0; i < board.length; i++) { if(board[i].equals("A")){ countOfA++; } } if(countOfA==0){ return 5; } else{ return 5 - countOfA; } } // 用户期望的打印方法 static void printboard(String[] board){ System.out.println(board[0] + "|" + board[1] + "|" + board[2] + "|" + board[3] + "|" + board[4] + "|" + board[5] + "|" + board[6] + "|" + board[7] + "|" + board[8] + "|" + board[9]); System.out.println(board[10] + "|" + board[11] + "|" + board[12] + "|" + board[13] + "|" + board[14] + "|" + board[15] + "|" + board[16] + "|" + board[17] + "|" + board[18] + "|" + board[19]); System.out.println(board[20] + "|" + board[21] + "|" + board[22] + "|" + board[23] + "|" + board[24] + "|" + board[25] + "|" + board[26] + "|" + board[27] + "|" + board[28] + "|" + board[29]); }
原代码问题分析
- 数组越界异常:在判断相邻位置是否为"A"时,未处理边界情况。当
j=0时,j-1=-1;当j=board.length-1时,j+1=board.length,直接访问这些索引会触发ArrayIndexOutOfBoundsException。 - 无法生成恰好5个"A":
- 随机选中的索引可能重复,导致多次设置同一个位置为"A",实际新增的"A"数量不足
- 循环逻辑混乱,
CountAInArray返回的数值无法准确控制需要新增的"A"数量,可能陷入死循环或添加过量
修正后的完整代码
public class AppleBoard { public static void main(String[] args) { // 初始化长度为29的数组,所有元素设为"-" String[] board = new String[29]; for (int i = 0; i < board.length; i++) { board[i] = "-"; } // 放置5个不相邻的"A" placeRandomAppleAroundBoard(board); // 打印数组 printboard(board); } static void placeRandomAppleAroundBoard(String[] board) { int placedCount = 0; // 直到放置满5个"A" while (placedCount < 5) { int j = (int) (Math.random() * board.length); // 检查当前位置是否为"-",且相邻位置没有"A" if (board[j].equals("-") && !hasAdjacentA(board, j)) { board[j] = "A"; placedCount++; } } } // 辅助方法:检查指定索引的相邻位置是否有"A" static boolean hasAdjacentA(String[] board, int index) { // 检查左边相邻(索引>0时) if (index > 0 && board[index - 1].equals("A")) { return true; } // 检查右边相邻(索引<数组长度-1时) if (index < board.length - 1 && board[index + 1].equals("A")) { return true; } return false; } static void printboard(String[] board) { // 打印第一行(索引0-9) StringBuilder line1 = new StringBuilder(); for (int i = 0; i <= 9; i++) { line1.append(board[i]).append("|"); } // 移除最后多余的"|" System.out.println(line1.deleteCharAt(line1.length() - 1)); // 打印第二行(索引10-19) StringBuilder line2 = new StringBuilder(); for (int i = 10; i <= 19; i++) { line2.append(board[i]).append("|"); } System.out.println(line2.deleteCharAt(line2.length() - 1)); // 打印第三行(索引20-28),注意数组长度是29,最大索引是28 StringBuilder line3 = new StringBuilder(); for (int i = 20; i <= 28; i++) { line3.append(board[i]).append("|"); } System.out.println(line3.deleteCharAt(line3.length() - 1)); } }
修正说明
- 解决数组越界问题:新增
hasAdjacentA辅助方法,先判断索引是否在有效范围内,再检查相邻位置是否为"A"。 - 保证恰好5个"A":通过计数器
placedCount跟踪已放置的"A"数量,每次仅在符合条件的位置(当前是"-"且无相邻"A")放置"A",直到计数器达到5。 - 优化打印方法:使用
StringBuilder拼接字符串,避免原代码中索引29的越界问题(数组长度29,最大索引是28)。
内容的提问来源于stack exchange,提问作者william beaudin
相关产品推荐
相关产品推荐

