我见过有类似问题的其他线程,但是在这些线程中,解决方案是创建一个带有值副本的最终变量,以便该变量实际上是最终的。但是,我的问题源于这样一个事实,即产生错误的变量是一个包含类实例的二维数组。
这是发生错误的 Controller 类的代码:
package Controller;
import Model.Die;
import View.GameWindow;
import View.HelpWindow;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Controller {
private GameWindow view;
private static int pos[] = new int[2];
private Die diceLayout[][];
private String createdWord;
public Controller(){
view = new GameWindow();
addHelpListener();
addSubmitListener();
diceLayout = view.getDice();
}
private void submitWord(String word){
boolean valid = checkValidWord(word);
if(valid){
System.out.println("The word ‘"+word+"‘ is valid.");
}else{System.out.println("The word ‘"+word+"‘ is not valid.");}
}
private boolean checkValidWord(String word){
boolean validSpell = validWordDic(word);
boolean connected = checkWordConnected(word);
if(validSpell&&connected){
return true;
}else{
return false;
}
}
private static boolean validWordDic(String word){
word=word.toLowerCase();
try {
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\sambe\\Desktop\\IPT\\BoggleGame\\res\\dictionary.txt"));
String str;
while ((str = in.readLine()) != null) {
if (str.indexOf(word) != -1) {
return true;
}
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public boolean checkWordConnected(String word){
word = word.toLowerCase();
String letters[] = word.split("");
for(int i = 0; i<4; i++){
for(int j = 0; j<4; j++){
if(letters[0]==diceLayout[i][j].getText()){
pos[0]=i;
pos[1]=j;
}
}
}
return true;
}
如果有人对我如何解决此问题有任何建议,将不胜感激。谢谢!
临摹微笑
斯蒂芬大帝
相关分类