我有一个项目的大学,需要我创建一个纵横字谜使用二维数组。我已经做了一些进展,但一直停留在如果用户正确地解决了难题,它就会不断循环的部分。我需要帮助打破了循环时,两个数组是完全相同的意思是难题已经解决了!先谢谢你了。
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),但似乎也不起作用。最后,我创建了一个方法来比较数组,我对此没有信心,因为这是最后的努力。
1条答案
按热度按时间anhgbhbe1#
我建议修改代码中的很多地方,特别是将一些东西移到main之外的许多不同方法中,以使代码更具可读性,更容易调试。
但我相信你的错误在于你如何检查数组相等。我不知道你在添加自己的
equals
方法之前是如何检查的,但是你的equals
方法不起作用。默认情况下,执行
myArray == myArray2
或myArray.equals(myArray2)
将返回引用equals,这意味着它们通过对象引用检查它们是否完全相同如果你不想写自己的方法,你可以使用Arrays库来匹配:
Arrays.deepEquals(myArray, myArray2)
您可以使用它来检查整个2D数组。举例来说:如果你有一张纸,上面写着“狗”,另一张写着“狗”,
myArray == myArray2
会告诉你它们不一样,因为它们是两张不同的纸,但是Arrays.deepEquals(myArray, myArray2)
会告诉你纸上的内容是一样的。如果你想写自己的方法,你需要一个嵌套的for循环来检查2D数组中每个char数组中的每个char: