Java 爪哇语;“缺少返回语句”;
这是我代码的一小部分。问题是,无论代码是什么,都会进入Java 爪哇语;“缺少返回语句”;,java,for-loop,compiler-errors,return,Java,For Loop,Compiler Errors,Return,这是我代码的一小部分。问题是,无论代码是什么,都会进入if语句 public static double value(char[][] array, int x, int y) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[0].length; j++) { if (array[i][j] == (char) 120) {
if
语句
public static double value(char[][] array, int x, int y) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == (char) 120) {
int x_cord = i;
int y_cord = j;
int width = (x_cord - x);
int height = (y_cord - y);
Math.abs(width);
Math.abs(height);
double distance = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
return distance;
}
}
}
}
公共静态双精度值(char[][]数组,int x,int y){
for(int i=0;i
我知道我在某个地方有一个逻辑缺陷,我需要以某种方式告诉程序,不管发生什么,它都会到达那里,但我可以这样做吗?代码中有两个可能的路径/分支,如果定义了方法返回值,则每个方法都必须有一个有效的返回语句。代码中有两个可能的路径/分支,如果定义了方法返回值,则每个方法都必须有一个有效的返回语句。在每个理论上可能的流中都必须有一个
返回
语句,即使在运行时没有机会到达那里。只需在末尾添加一个“虚拟返回”,您就可以:
public static double value(char[][] array, int x, int y) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == (char) 120) {
int x_cord = i;
int y_cord = j;
int width = (x_cord - x);
int height = (y_cord - y);
Math.abs(width);
Math.abs(height);
double distance = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
return distance;
}
}
}
return 0; // Or some other default
}
公共静态双精度值(char[][]数组,int x,int y){
for(int i=0;i
在每个理论上可能的流中都必须有一个return
语句,即使在运行时不可能到达该语句。只需在末尾添加一个“伪return”,您就可以:
public static double value(char[][] array, int x, int y) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == (char) 120) {
int x_cord = i;
int y_cord = j;
int width = (x_cord - x);
int height = (y_cord - y);
Math.abs(width);
Math.abs(height);
double distance = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
return distance;
}
}
}
return 0; // Or some other default
}
公共静态双精度值(char[][]数组,int x,int y){
for(int i=0;i
尝试创建区域设置变量双距离=0代码>初始化如下:
public static double value(char[][] array, int x, int y) {
double distance= 0;
...
return distance;
}
}
}
return distance;
}
尝试创建一个区域设置变量double distance=0代码>初始化如下:
public static double value(char[][] array, int x, int y) {
double distance= 0;
...
return distance;
}
}
}
return distance;
}
如果编译器不确定是否会接受非void
返回类型的方法,那么它将不接受
- 返回一个值或
- 抛出异常
由于您确信循环中的return
语句始终被执行,因此可以添加带有任何值的return
语句或引发异常:
public static double value(char[][] array, int x, int y) {
for (int i = 0; i < array.length; i++) {
...
}
throw new IllegalArgumentException("array must contain a char with code 120");
}
公共静态双精度值(char[][]数组,int x,int y){
for(int i=0;i
如果编译器不确定是否会接受非void
返回类型的方法,则编译器不会接受该方法
- 返回一个值或
- 抛出异常
由于您确信循环中的return
语句始终被执行,因此可以添加带有任何值的return
语句或引发异常:
public static double value(char[][] array, int x, int y) {
for (int i = 0; i < array.length; i++) {
...
}
throw new IllegalArgumentException("array must contain a char with code 120");
}
公共静态双精度值(char[][]数组,int x,int y){
for(int i=0;i
如果数组
为空怎么办?那样的话,你需要归还一些东西。或引发异常。您必须从此方法返回。现在,仅当codition为true时才返回。如果array
为空怎么办?那样的话,你需要归还一些东西。或引发异常。您必须从此方法返回。现在,仅当codition为true时才返回。应该注意的是,通过使用此模式,值0被赋予了特殊含义。在这种情况下,这可能是合适的,但它可能会引入一些微妙的bug,我觉得这不是什么好东西。也就是说,根据手头的信息,我看不到任何其他解决方案。应该注意的是,通过使用此模式,值0被赋予了特殊含义。在这种情况下,这可能是合适的,但它可能会引入一些微妙的bug,我觉得这不是什么好东西。也就是说,考虑到手头的信息,我看不到任何其他解决方案。