![](/img/trans.png)
[英]Error: Incompatible String Types: Unexpected return value - This is the error of my code when I return a string
[英]error: incompatible types: unexpected return value Char compare to String
我正在嘗試將char與String進行比較,但出現錯誤。 錯誤
error: incompatible types: unexpected return value
碼:
public class MyClass {
public static void main(String args[]) {
String s = Integer.toBinaryString(2432);
String p = "0";
for (char c : s.toCharArray()) {
return p.equals(new String(new char[]{c}));
}
}
}
main()
的類型為void
( public static
void main(String args[]) {
),因此您不能從中返回值。
運行此命令時得到的實際錯誤是:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Void methods cannot return a value
字符不是對象,可以/應該使用==
運算符進行比較。 無需每次都構建一個字符串。 同樣main
方法不能返回任何值,它是void
。
main方法不能返回任何內容,它具有void
類型。 您可以編寫一個單獨的函數來返回該值,將該值分配給變量或僅打印該值。
public boolean strChar(String s, String p){
return p.equals(new String(new char[]{c}));
}
要么
System.out.println(p.equals(new String(new char[]{c})));
要么
boolean equals = p.equals(new String(new char[]{c}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.