【发布时间】:2017-04-13 01:54:15
【问题描述】:
我知道这是一些代码,但它真的让我发疯:( 它不断抛出 dfs() 方法在第 31 行期望返回类型的错误,但是它被声明为返回 void!这里发生了什么?我似乎检查了所有括号,范围看起来不错,不知道是什么原因造成的! 感谢您的帮助!
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 的“格式化源代码”功能。我相信你会在某处找到缺少的
}或多余的{。 -
问题出在 if 块中,当您递归地尝试调用“dfs”方法时,您传递的参数类型/数量不正确。这 --> dfs(r-1, c, board, word);是问题所在。确保添加所需数量的参数并确保类型也匹配。
-
你有几个编译错误,第一个是
BufferedReader boardLoad = new BufferedReader(new FileReader(new File(args[0])));- 接下来有点大,你的递归 dfs 调用与方法签名不匹配。