无法从int转换为boolean? [英] Cannot convert from int to boolean?

查看:83
本文介绍了无法从int转换为boolean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void main(String[] args) {
    int [] newArray= new int [4];
    int [] array = {4,5,6,7};
    oddEven(array);

    newArray[0] = array[0]+array[1]+array[2]+array[3];
    newArray[1] = array[0]*array[1]*array[2]*array[3];
}

public static void oddEven(int [] oddEven) {

    for (int i=0; i<oddEven.length; i++) {
        //  Cannot convert from int to boolean
        if (oddEven[i] % 2)

    }
}

忽略我要在此处管理的内容.我只是很好奇为什么它在此for循环中不接受"if"语句.正如我所说的那样,无法从int转换为boolean".您为什么这么说?

Ignore what I'm trying to manage here. I'm only curious why it doesn't accept "if" statement in this for loop. As I stated it says "cannot convert from int to boolean". Why do you think it says so?

推荐答案

应该将 if 旁边的表达式实现为 boolean 值,否则将导致java中的编译错误.

That expression in side the if should be realizes to boolean value, otherwise compilation error in java.

尝试

       if (oddEven[i] % 2 ==0)  {
       }

甚至(根据要求)

       if (oddEven[i] % 2 !=0)  {
       }

请参见语言规范#一章14

表达式必须具有布尔型或布尔型,否则会发生编译时错误.

The Expression must have type boolean or Boolean, or a compile-time error occurs.

这篇关于无法从int转换为boolean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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