Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不兼容类型错误。JAVA_Java_Incompatibletypeerror - Fatal编程技术网

不兼容类型错误。JAVA

不兼容类型错误。JAVA,java,incompatibletypeerror,Java,Incompatibletypeerror,我试图在java中返回一个整数,但它给出了不兼容类型错误 public int getNubers(){ if (counter.getValue()% 9 == 0) { return "counter.getValue/9"; } } counter.getValue/9是一个字符串,而不是int。 应该是 return counter.getValue()/9; 函数的声明返回类型为int,但您正在尝试返回字符串。尝试一下: public String getNube

我试图在java中返回一个整数,但它给出了不兼容类型错误

public int getNubers(){
 if (counter.getValue()% 9 == 0) 
 {
     return "counter.getValue/9";
 }
}
counter.getValue/9是一个字符串,而不是int。 应该是

return counter.getValue()/9;

函数的声明返回类型为int,但您正在尝试返回字符串。尝试一下:

public String getNubers(){
 if (counter.getValue()% 9 == 0) 
 {
     return "counter.getValue/9";
 }
 return "OOPS"; //gotta return something here.
}
或者,我认为您实际上正在尝试这样做:

public int getNubers(){
 if (counter.getValue()% 9 == 0) 
 {
     return counter.getValue() / 9 ;
 }
 return -1; //bloop bleep
}

无论哪种方式,这两个答案都有部分错误。如果你有一个函数返回某个东西,不管发生什么,你都应该返回它。因此,如果除法返回一个整数,只返回整数是不可取的

如果除法不是一个整数,比如-1,您可以指定一个错误值,然后在函数末尾返回该值,这意味着除法返回的不是一个整数

如果除法不能得到整数,也可以抛出异常


祝你好运。

public int return int not string..public string return string我想知道什么是数字。它有点像一个数字,但对于m,不管它是什么意思?谢谢你们,汉斯,这很有帮助。汉斯·莱波特的回答是对的。该代码不会编译,因为if语句之后没有return语句。