提问人:Patrona7 提问时间:9/28/2023 更新时间:9/29/2023 访问量:54
当两个 2D 数组变得完全相同时,如何打破 do while 循环?
How do I break a do while loop when two 2D arrays become exactly the same?
问:
我有一个 uni 项目,需要我使用 2 维数组创建一个填字游戏。我已经取得了一些进展,但坚持如果用户正确解决难题,它就会继续循环。当两个数组完全相同时,我需要帮助来打破循环,这意味着谜题已经解决!先谢谢你。
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 wrote in full caps!.");
char[][] correctPuzzle = {{' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '},
{'|','C','|', 'H', '|', 'A', '|','R', '|', '■', '|'},
{'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
{'|','L','|', 'O', '|', 'O', '|','P', '|', '■', '|'},
{'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
{'|','A','|', '■', '|', '■', '|','■', '|', 'I', '|'},
{'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
{'|','S','|', 'U', '|', 'M', '|','■', '|', 'N', '|'},
{'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
{'|','S','|', '■', '|', '■', '|','■', '|', 'T', '|'},
{' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '},};
char[][] defaultPuzzle = {{' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '},
{'|',' ','|', ' ', '|', ' ', '|',' ', '|', '■', '|'},
{'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
{'|',' ','|', ' ', '|', ' ', '|',' ', '|', '■', '|'},
{'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
{'|',' ','|', '■', '|', '■', '|','■', '|', ' ', '|'},
{'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
{'|',' ','|', ' ', '|', ' ', '|','■', '|', ' ', '|'},
{'|','—','—', '—', '—', '—', '—','—', '—', '—', '|'},
{'|',' ','|', '■', '|', '■', '|','■', '|', ' ', '|'},
{' ','—','—', '—', '—', '—', '—','—', '—', '—', ' '},};
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? ");
Scanner sc = new Scanner(System.in);
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!!!");
}
System.out.println("If you want to continue and see your progress, press 1");
System.out.println("Is you are struggling and want to see the correct answers, press 2");
option = sc.nextInt();
}
while(option == 1);
if(option == 2) {
for (int i = 0; i < 11; i++) {
System.out.println();
for (int j = 0; j < 11; j++) {
System.out.print(correctPuzzle[i][j] + " ");
}
}
System.out.println("Better luck next time! These were the correct answers!");
}
else if (equals(correctPuzzle, defaultPuzzle)) {
System.out.print("Congratulations! 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] + " ");
}
}
}
}
public static boolean equals(char[][] a, char[][] b) {
if (a.length != b.length)
return false;
for(char i = 0; i<a.length; i++) {
if (a[i] != b[i])
return false;
}
return true;
}
}
我尝试添加第 6 个 switch 语句,如果数组匹配,它将中断,但它不起作用。我尝试向while添加条件(option == 1 && correctPuzzle == defaultPuzzle),但这似乎也不起作用。最后,我创建了一个比较数组的方法,我对此没有信心,因为这是最后的手段。
答:
我建议对代码进行很多更改,特别是将内容移动到 main 之外的许多不同方法中,以使您的代码更具可读性和更易于调试。
但我相信你的错误在于你如何检查数组是否相等。我不知道您在添加自己的方法之前是如何检查它的,但您的方法不起作用。equals
equals
默认情况下,执行 or 将返回引用等于,这意味着它们通过对象引用检查它们是否是完全相同的数组myArray == myArray2
myArray.equals(myArray2)
如果你不想编写自己的方法,你可以使用 Arrays 库进行匹配:你可以用它来检查整个 2D 数组。Arrays.deepEquals(myArray, myArray2)
例如:如果你有一张纸(paper1),上面有“dog”这个词,而另一张(paper2)上面有“dog”这个词,会告诉你它们不一样,因为它们是两张不同的纸,但会告诉你纸上的内容是一样的。paper1 == paper2
Arrays.deepEquals(paper, paper2)
如果你想编写自己的方法,你将需要一个嵌套的 for 循环来检查 2D 数组中每个 char 数组中的每个 char:
public static boolean equals(char[][] a, char[][] b){
for(int row = 0; row < a.length; row++){
for(int col = 0; col < a[row].length; col++){
if(a[row][col] != b[row][col]) return false;
}
}
return true;
}
评论