错误:不兼容的类型:意外的返回值 Char 与 String 比较 [英] error: incompatible types: unexpected return value Char compare to String

查看:239
本文介绍了错误:不兼容的类型:意外的返回值 Char 与 String 比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 char 与 String 进行比较,但出现错误.错误

I am trying to compare char to a String but getting an error. Error

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[]) {)因此你不能从中返回一个值.

The type of main() is void (public static void main(String args[]) {) and therefore you cannot return a value from it.

我运行时得到的实际错误是:

The actual error I get when I run this is:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Void methods cannot return a value

这篇关于错误:不兼容的类型:意外的返回值 Char 与 String 比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆