java 类型不匹配:无法从 int 转换为 String
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15737043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type mismatch: cannot convert from int to String
提问by Philip Hung
public String getMessage (int numEggs) {
int a = numEggs/12;
int b = numEggs%12;
if ( numEggs < 0) {
System.out.println("Invalid number");
} else {
System.out.println("Your number of eggs is "+ a +" dozen(s) and "+ b+".");
return;
}
}
So I keep getting Type mismatch: cannot convert from int to Stringwhen I try to put something in the return; what's wrong with the code? I have to use getMessage (int numEggs)as it is part of the question I was given.
因此,Type mismatch: cannot convert from int to String当我尝试将某些东西作为回报时,我会不断得到回报;代码有什么问题?我必须使用,getMessage (int numEggs)因为它是我被问到的问题的一部分。
采纳答案by Iswanto San
I don't get Type mismatch: cannot convert from int to Stringbut the error is: This method must return a result of type String
我不明白,Type mismatch: cannot convert from int to String但错误是:This method must return a result of type String
You're missing return statement in your method:
您的方法中缺少 return 语句:
public String getMessage(int numEggs) {
int a = numEggs / 12;
int b = numEggs % 12;
if (numEggs < 0) {
return "Invalid number";
} else {
return "Your number of eggs is " + a + " dozen(s) and "
+ b + ".";
}
}
Even I change the getMessagereturn type to voidit doesn't give me Type mismatch: cannot convert from int to String
即使我将getMessage返回类型更改为void它也不会给我Type mismatch: cannot convert from int to String
public void getMessage(int numEggs) {
int a = numEggs / 12;
int b = numEggs % 12;
if (numEggs < 0) {
System.out.println("Invalid number");
} else {
System.out.println("Your number of eggs is " + a + " dozen(s) and "
+ b + ".");
return;
}
}
回答by camickr
cannot convert from int to String" when i try to put something in the return,
无法从 int 转换为 String”,当我尝试将某些内容放入返回中时,
The method expects you to return a String. So you can't do:
该方法期望您返回一个字符串。所以你不能这样做:
return 1; //ie 1, does not get automatically converted to "1"
But you can do:
但你可以这样做:
return "I'm a String";
回答by corvid
Do you need to return a string? if you just desire to print, simply make the return type void and remove the "return" at the bottom.
你需要返回一个字符串吗?如果您只想打印,只需将返回类型设为无效并删除底部的“返回”。
回答by Joseph Selvaraj
If you want to return a String:
如果你想返回一个字符串:
public String getMessage (int numEggs) {
公共字符串 getMessage (int numEggs) {
int a = numEggs/12;
int b = numEggs%12;
String strReturn = "";
if ( numEggs < 0) {
strReturn = "Invalid number";
} else {
strReturn = "Your number of eggs is "+ a +" dozen(s) and "+ b+".";
}
return strReturn;
}
}
If you want to print it in the consolse then
如果你想在控制台中打印它然后
public void getMessage (int numEggs) {
公共无效getMessage(int numEggs){
int a = numEggs/12;
int b = numEggs%12;
if ( numEggs < 0) {
System.out.println("Invalid number");
} else {
System.out.println("Your number of eggs is "+ a +" dozen(s) and "+ b+".");
}
}
}

