-1
我知道這一點的代碼,但它真的快把我逼瘋了:( 它不斷拋出這個方法DFS()期待在第31行返回類型的錯誤,但它被聲明爲返回一個無效!這到底是怎麼回事?我似乎已經檢查了所有的括號和範圍看起來不錯IDK的是什麼原因造成這個! 感謝您的幫助!錯誤:無效的方法聲明;返回類型要求(第31行)
import java.util.*;
import java.io.*;
public class Project10 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(new File("dictionary.txt")));
BufferedReader boardLoad = new BufferedReader(args[0]);
ArrayList<String> dictionary = new ArrayList<String>();
HashSet<String> hashDict = new HashSet<String>();
while (br.ready()) {
String word = br.readLine();
dictionary.add(word);
hashDict.add(word);
}
Collections.sort(dictionary);
int boardSize = Integer.parseInt(boardLoad.readLine());
String[][] board = new String[boardSize][boardSize];
for (int i = 0; i < boardSize - 1; i++) {
for (int j = 0; j < boardSize - 1; i++) {
board[i][j] = boardLoad.read();
}
}
int row = 0;
int col = 0;
String word = "";
dfs(row, col, board, dictionary, hashDict, word);
}
private static void dfs(int r, int c, String[][] board,
ArrayList<String> dict, HashSet set, String word) {
word += board[r][c];
boolean isWord = isWord(set, word);
boolean isPrefix = isPrefix(dict, word);
if(!isWord && !isPrefix) {
return;
} else if (isWord) {
System.out.println(word);
}
if (r != 0 && isLowerCase(board[r-1][c])) {
board[r][c] = board[r][c].toUpperCase();
dfs(r-1, c, board, word);
board[r][c] = board[r][c].toLowerCase();
}
if (r != 0 && c != board.length - 1 && isLowerCase(board[r-1][c+1])) {
board[r][c] = board[r][c].toUpperCase();
dfs(r-1, c+1, board, word);
board[r][c] = board[r][c].toLowerCase();
}
if (c != board.length - 1 && isLowerCase(board[r][c+1])) {
board[r][c] = board[r][c].toUpperCase();
dfs(r, c+1, board, word);
board[r][c] = board[r][c].toLowerCase();
}
if (r != board.length && c != board.length - 1 && isLowerCase(board[r+1][c+1])) {
board[r][c] = board[r][c].toUpperCase();
dfs(r+1, c+1, board, word);
board[r][c] = board[r][c].toLowerCase();
}
if (r != board.length - 1 && isLowerCase(board[r+1][c])) {
board[r][c] = board[r][c].toUpperCase();
dfs(r+1, c, board, word);
board[r][c] = board[r][c].toLowerCase();
}
if (r != board.length - 1 && c != 0 && isLowerCase(board[r+1][c-1])) {
board[r][c] = board[r][c].toUpperCase();
dfs(r+1, c-1, board, word);
board[r][c] = board[r][c].toLowerCase();
}
if (c != 0 && isLowerCase(board[r][c-1])) {
board[r][c] = board[r][c].toUpperCase();
dfs(r, c-1, board, word);
board[r][c] = board[r][c].toLowerCase();
}
if (r != 0 && c != 0 && isLowerCase(board[r-1][c-1])) {
board[r][c] = board[r][c].toUpperCase();
dfs(r-1, c-1, board, word);
board[r][c] = board[r][c].toLowerCase();
}
}
private static boolean isWord(HashSet<String> hashDict, String word) {
return hashDict.contains(word);
}
private static boolean isPrefix(ArrayList<String> dictionary, String word) {
int lo = 0;
int hi = dictionary.size() - 1;
while(lo <= hi) {
int mid = lo + (hi - lo)/2;
if (dictionary.get(mid).startsWith(word)) {
return true;
} else if (dictionary.get(mid).compareTo(canonWord) < 0) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return false;
}
private static boolean isLowerCase(String str) {
return str.equals(str.toLowerCase());
}
}
請開始與正確縮進你的代碼 - 無論是通過手工或最好使用IDE的「格式化源代碼」功能。我相信你會在某處找到缺少'}'或超級''''的。 –
問題是在您若塊(S)當您試圖遞歸調用「DFS」的方法,你傳遞的參數不正確的類型/數量。 this - > dfs(r-1,c,board,word);是問題。確保你添加了所需數量的參數並確保類型匹配。 –
你有幾個編譯錯誤,首先是'的BufferedReader boardLoad =新的BufferedReader(新的FileReader(新文件(參數[0])));' - 未來是種很大的,你的遞歸調用DFS不匹配的方法簽名。 –