【问题标题】:error: invalid method declaration; return type required (line 31)错误:无效的方法声明;需要返回类型(第 31 行)
【发布时间】: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 调用与方法签名不匹配。

标签: java types return


【解决方案1】:

您的代码有很多编译器错误,并且 dfs() 方法没有任何关于第 31 行期望返回类型的信息。

您的代码中的相同编译器错误可以这样纠正:

//First line to correct
BufferedReader boardLoad = new BufferedReader(new FileReader(new File(args[0])));

//Second line to correct
board[i][j] = String.valueOf(boardLoad.read());

//Next N lines to correct
dfs(r - 1, c + 1, board, dict, set,word);

我建议您使用任何 IDE 来帮助解决您的问题

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多