简体   繁体   English

如何解决“类型不匹配:无法从 int 转换为 boolean”java 错误?

[英]How to resolve “Type mismatch: cannot convert from int to boolean” java error?

When I'm trying to run one of the examples on the book I get an error which I don't know how to fix.当我尝试运行本书中的一个示例时,我收到了一个我不知道如何修复的错误。 The code is:代码是:

class Xcopy {
    public static void main(String[] args) {
        int org = 42;
        Xcopy x = new Xcopy();
        int y = x.jazda(org);
        System.out.println(org + " " + y);
    }
    void jazda(int arg) {
        arg = arg * 2;
        return arg;
    }
}

Your method jazda is of type void and returns int .您的方法jazda是 void 类型并返回int

class Xcopy {
    public static void main(String[] args) {
        int org = 42;
        Xcopy x = new Xcopy();
        int y = x.jazda(org);
        System.out.println(org + " " + y);
    }
    void jazda(int arg) { //you declare returning type of method to void
        arg = arg * 2;
        return arg; //you return int
    }
}

You need to change type of method jazda to int .您需要将方法jazda的类型更改为int

class Xcopy {
    public static void main(String[] args) {
        int org = 42;
        Xcopy x = new Xcopy();
        int y = x.jazda(org);
        System.out.println(org + " " + y);
    }
    int jazda(int arg) { //changed return type to int
        arg = arg * 2;
        return arg;
    }
}

Now all methods and variables got corresponding types.现在所有的方法和变量都得到了相应的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM