比较多个布尔值 [英] Compare multiple boolean values

查看:83
本文介绍了比较多个布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从方法中返回了三个布尔值,我想检查这样的条件:首先它将检查所有三个布尔值

i have three boolean value returning from the method , i want to check condition like this :First It will check all the three boolean value

场景1:如果locationMatch,matchCapacity,filterMatchStatus则语句返回真值.

Scenario 1: if locationMatch, matchCapacity, filterMatchStatus then statement return true value.

场景2:如果locationMatch,matchCapacity,filterMatchStatus如果布尔值是false,则返回false值

Scenario 2: if locationMatch, matchCapacity, filterMatchStatus if any boolean is false then it return false value

我曾这样尝试过,但是,如果任何布尔值为true,它将返回true

I tried like this but , it is returning true if any boolean value is true

public boolean matchFilter(FilterTruck filter){

            boolean locationMatch = filterMatchesLocation(filter);
            boolean matchCapacity = filterMatchesCapacity(filter);
            boolean filterMatchStatus = filterMatchesStatus(filter);
            if (locationMatch) {
                return true;
            }
            if (matchCapacity) {
                return true;
            }
            if (filterMatchStatus) {
                return true;
            }
        }
        return false;
    }

推荐答案

更新了您的代码,试试这个.

Updated your code try this.

public boolean matchFilter(FilterTruck filter) {

    boolean locationMatch = filterMatchesLocation(filter);
    boolean matchCapacity = filterMatchesCapacity(filter);
    boolean filterMatchStatus = filterMatchesStatus(filter);

    return locationMatch && matchCapacity && filterMatchStatus;
}

这篇关于比较多个布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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