简体   繁体   English

If语句中的布尔相等运算符

[英]boolean equality operator in If statement

I'm trying to create a if statement in which X may be equal to multiple values. 我正在尝试创建一个if语句,其中X可能等于多个值。 Basically, I want something like 基本上,我想要类似

if(month == (2||4||6||9||11)); 

But, I can't do that since int can't go along with "||" 但是,我不能这样做,因为int不能与“ ||”一起使用 operator. 操作员。 I would like to know how to create a if statement like that. 我想知道如何创建一个if语句。

You can't use it that way, as the || 您不能以这种方式使用它,因为|| operator requires boolean operands (and you are using integers). 运算符需要boolean操作数(并且您正在使用整数)。

Your alternatives are: 您的替代方法是:

An if statement with each equality expression separated: 每个相等表达式分开的if语句:

if (month == 2 || month == 4 || month == 6 || month == 9 || month == 11) {
    // your stuff here
}

A switch statement with fall through : 一个switch语句到秋季

switch (month) {
    case 2:
    case 4:
    case 8:
    case 9:
    case 11:
       // your stuff here
       break;
    default:
       // do nothing
       break;
}

(But use fall through with care, some may consider it a bad practice - as it can make your code's intentions less clear.) (但是使用时要格外小心,有些人可能会认为这是一种不好的做法-因为它会使您的代码意图不那么清楚。)

Using a temporary collection and the contains() method: 使用临时集合contains()方法:

if (java.util.Arrays.asList(2, 4, 6, 9, 11).contains(month)){
    // your stuff here
}

The use of one or other alternative, of course, depends on personal opinion and number of operands. 当然,使用一种或其他方式取决于个人意见和操作数。



As a side note on code readability , though, my suggestion would be to get the meaning of that expression and create a method for that, which would make your code easier to understand and maintain. 不过,作为代码可读性的补充说明,我的建议是获取该表达式的含义并为此创建一个方法,这将使您的代码更易于理解和维护。

So, say those months are months with "full price" (or anything that makes sense to your business rules). 因此,假设这些月份是“全价”(或对您的业务规则有意义的月份)的月份。 I'd use: 我会用:

if (isFullPricedMonth(month)) {
    // your stuff here
}

And, of course, the method (with any of the solutions previously posted): 当然,该方法(带有以前发布的任何解决方案):

public boolean isFullPricedMonth(int month) {
    return month == 2 || month == 4 || month == 6 || month == 9 || month == 11;
}
int a = 2;
if (a == 3 || a == 4) {
    //do stuff
}
List<Integer> months = Arrays.asList(2, 4, 6, 9, 11);
if(months.contains(month)){
   // do something
}

You have to compare each value with month and separate them with || 您必须将每个值与月份进行比较,并用||将它们分开。

if(month == 2 ||month == 4 ||month == 6 ||month == 9 ||month == 11);

I recommend you use a switch statement. 我建议您使用switch语句。

switch (month){
    case 2:
    case 4:
    case 6:
    case 9:
    case 11:
    //do stuff here
}

You can ask whether the car is black or white in english. 您可以问汽车英语是黑色还是白色。 You cannot do this in most of the programming languages. 您不能使用大多数编程语言来执行此操作。 In programming languages, you have to ask whether the car is black or the car is white: 在编程语言中,您必须询问汽车是黑色还是白色:

if(month == 2 || month == 4 /* || month == ... */ )

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

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