简体   繁体   中英

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 .

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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