我有以下代码:
public class BinaryDecoder{
public static void main(String[] args){
String binNum = "110101";
int res = 0;
for (int i = 0;i<binNum.length();i++){
//res += parseDigit(binNum.charAt(i),binNum);
}
// System.out.println(res);
}
public static int parseDigit(int index, String binNum){
switch (binNum.charAt(index)){
case 0:
break;
case 1:
int val = (int) Math.pow(2,-index+binNum.length());
return val;
}
}
}
我收到错误:BinaryDecoder.java:30: 错误:缺少返回语句 } ^
我可能解释错误了,但是为什么我的类 BinaryDecode 有甚至需要 return 语句?代码还有其他问题吗?
注意:我对 java 还很陌生,如果我的代码中有任何明显的错误,我很抱歉。
最佳答案
需要 return 语句的不是类;而是类。它是 parseDigit
函数,您已将其声明为返回 int
。当您说过会返回值时,却有可能不返回值,这是一个错误。
关于Java 要求类的返回语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29000056/