Java中如何在两个二维数组完全相同时终止do-while循环?
填字游戏循环终止问题解决方案
核心问题分析
你的代码主要有两个致命问题导致填对后无法终止循环:
- 自定义的
equals方法错误比较一维数组引用,而非逐个字符匹配; do-while循环条件未加入数组匹配判断,仅依赖用户输入的option值。
分步修复方案
1. 修复二维数组比较方法
原方法仅判断一维数组的引用地址,完全没比较实际字符内容。替换为以下实现:
public static boolean equals(char[][] a, char[][] b) { // 先比较行数是否一致 if (a.length != b.length) return false; // 遍历每一行,比较列数和每个字符 for (int i = 0; i < a.length; i++) { if (a[i].length != b[i].length) return false; for (int j = 0; j < a[i].length; j++) { if (a[i][j] != b[i][j]) return false; } } return true; }
2. 修改循环终止条件
在do-while的循环条件中加入数组匹配判断,只要填对所有内容就终止循环:
do { // 原有循环逻辑... } while (option == 1 && !equals(correctPuzzle, defaultPuzzle));
3. 优化循环结束后的逻辑
调整判断顺序,优先检查是否完成填字,确保用户完成后能直接显示祝贺信息:
if (equals(correctPuzzle, defaultPuzzle)) { System.out.println("\nCongratulations! You have solved the puzzle"); // 打印完成的填字板 for (int i = 0; i < 11; i++) { System.out.println(); for (int j = 0; j < 11; j++) { System.out.print(correctPuzzle[i][j] + " "); } } } else if (option == 2) { // 显示正确答案的逻辑 System.out.println("\nBetter luck next time! These were the correct answers!"); for (int i = 0; i < 11; i++) { System.out.println(); for (int j = 0; j < 11; j++) { System.out.print(correctPuzzle[i][j] + " "); } } }
4. 修复其他语法错误
- 补全开头打印语句的闭合双引号:
System.out.println("Each letter must be in place of the empty space. Each letter must be written in full caps!"); - 补全
correctPuzzle和defaultPuzzle初始化末尾的分号; - 将
Scanner实例移到循环外,避免重复创建,循环结束后调用sc.close()关闭资源。
完整修正代码
import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("HI! This is a crossword puzzle using 2D arrays in JAVA!"); System.out.println("There are 5 words in total, that you have to guess!"); System.out.println("Each letter must be in place of the empty space. Each letter must be written in full caps!"); char[][] correctPuzzle = {{' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '}, {'|','C','|', 'H', '|', 'A', '|','R', '|', '■', '|'}, {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'}, {'|','L','|', 'O', '|', 'O', '|','P', '|', '■', '|'}, {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'}, {'|','A','|', '■', '|', '■', '|','■', '|', 'I', '|'}, {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'}, {'|','S','|', 'U', '|', 'M', '|','■', '|', 'N', '|'}, {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'}, {'|','S','|', '■', '|', '■', '|','■', '|', 'T', '|'}, {' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '}}; char[][] defaultPuzzle = {{' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '}, {'|',' ','|', ' ', '|', ' ', '|',' ', '|', '■', '|'}, {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'}, {'|',' ','|', ' ', '|', ' ', '|',' ', '|', '■', '|'}, {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'}, {'|',' ','|', '■', '|', '■', '|','■', '|', ' ', '|'}, {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'}, {'|',' ','|', ' ', '|', ' ', '|','■', '|', ' ', '|'}, {'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'}, {'|',' ','|', '■', '|', '■', '|','■', '|', ' ', '|'}, {' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '}}; Scanner sc = new Scanner(System.in); int option; do{ // 打印当前填字板 for (int i = 0; i < 11; i++) { System.out.println(); for (int j = 0; j < 11; j++) { System.out.print(defaultPuzzle[i][j] + " "); } } System.out.println(); // 显示问题 System.out.println("Question 1 (1st column) What is blueprint for an object?"); System.out.println("Question 2 (1st row) What data type represents a single character?"); System.out.println("Question 3 (2nd row) Feature used to execute a particular part of the program repeatedly"); System.out.println("Question 4 (4th row) Synonym for the word result - used in programming"); System.out.println("Question 5 (5th column) Abbreviation of the word integer"); System.out.println(); // 用户输入答案编号和内容 System.out.println("Which question would you like to answer? "); int ansNum = sc.nextInt(); System.out.println("Input your guess! "); String guess; switch(ansNum) { case 1: guess = sc.next(); if(guess.length() == 5) { for(int rowNo = 1, charNo = 0; charNo < guess.length(); rowNo+=2, charNo++){ defaultPuzzle[rowNo][1] = guess.charAt(charNo); } } else { System.out.println("Incorrect answer length!"); } break; case 2: guess = sc.next(); if(guess.length() == 4) { for(int collNo = 1, charNo = 0; charNo < guess.length(); collNo+=2, charNo++){ defaultPuzzle[1][collNo] = guess.charAt(charNo); } } else { System.out.println("Incorrect answer length!"); } break; case 3: guess = sc.next(); if(guess.length() == 4) { for(int collNo = 1, charNo = 0; charNo < guess.length(); collNo+=2, charNo++){ defaultPuzzle[3][collNo] = guess.charAt(charNo); } } else { System.out.println("Incorrect answer length!"); } break; case 4: guess = sc.next(); if(guess.length() == 3) { for(int collNo = 1, charNo = 0; charNo < guess.length(); collNo+=2, charNo++){ defaultPuzzle[7][collNo] = guess.charAt(charNo); } } else { System.out.println("Incorrect answer length!"); } break; case 5: guess = sc.next(); if(guess.length() == 3) { for(int rowNo = 5, charNo = 0; charNo < guess.length(); rowNo+=2, charNo++){ defaultPuzzle[rowNo][9] = guess.charAt(charNo); } } else { System.out.println("Incorrect answer length!"); } break; default: System.out.println("This answer doesn't exist. Please select 1-5!!!"); } // 提前判断是否完成,若完成直接终止循环 if (equals(correctPuzzle, defaultPuzzle)) { option = 0; break; } // 用户选择是否继续 System.out.println("If you want to continue and see your progress, press 1"); System.out.println("If you are struggling and want to see the correct answers, press 2"); option = sc.nextInt(); } while (option == 1 && !equals(correctPuzzle, defaultPuzzle)); // 循环结束后的结果展示 if (equals(correctPuzzle, defaultPuzzle)) { System.out.println("\nCongratulations! You have solved the puzzle"); for (int i = 0; i < 11; i++) { System.out.println(); for (int j = 0; j < 11; j++) { System.out.print(correctPuzzle[i][j] + " "); } } } else if (option == 2) { System.out.println("\nBetter luck next time! These were the correct answers!"); for (int i = 0; i < 11; i++) { System.out.println(); for (int j = 0; j < 11; j++) { System.out.print(correctPuzzle[i][j] + " "); } } } sc.close(); } public static boolean equals(char[][] a, char[][] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) { if (a[i].length != b[i].length) return false; for (int j = 0; j < a[i].length; j++) { if (a[i][j] != b[i][j]) return false; } } return true; } }
内容的提问来源于stack exchange,提问作者Patrona7
相关产品推荐
相关产品推荐

