如果条件java添加系列

时间:2013-02-09 10:19:48

标签: java if-statement

我想在if条件下添加系列

if (count == 33 || count == 66 || count == 99
            || count == 132 || count == 165 || count == 198
            || count == 231 || count == 264 || count == 297
            || count == 330 || count == 363 || count == 396
            || count == 429 || count == 462 || count == 495
            || count == 528 || count == 561 || count == 594
            || count == 627 || count == 660 || count == 693
            || count == 726 || count == 759 || count == 792
            || count == 825 || count == 858 || count == 891
            || count == 924 || count == 957 || count == 990
            || count == 1023 || count == 1056 || count == 1089){
        retValue = true;
    }else {
        retValue = false;
    }

有没有最好的方法呢?

6 个答案:

答案 0 :(得分:7)

所以,基本上你是通过33检查可分性,然后用模数运算符*来做那个:

if (count % 33 == 0) {
        retValue = true;
} else {
        retValue = false;
}

如果您想确认该范围,可以将其添加到条件:

if (count >= 33 && count <= 1089 && count % 33 == 0)

此外,您可以将if-else块简化为仅return个语句:

return count >= 33 && count <= 1089 && count % 33 == 0;

答案 1 :(得分:0)

尝试使用开关案例来解决您的问题

答案 2 :(得分:0)

使用ArrayList<Integer>,用整数值填充它。然后简单地说:

list.contains(value)

答案 3 :(得分:0)

if (count <=1089 && count%33==0){
   return true;
else
 return false;

答案 4 :(得分:0)

 List<int> list = new ArrayList<int>() { 33, 66, 99, 132, 165, 1198 };// add your numbers to this List, this can be reuse in another parts of Program 

            if (list.Contains(count))
            {
                retValue = true;
                System.out.ptinrtln("Element Found");

            }
            else
            {
                retValue = false;
                 System.out.ptinrtln("Element Not Found");
            }

答案 5 :(得分:0)

如上所述,您可以检查%33,但如果确实需要检查一些任意条件列表,您可以迭代创建答案

retValue = false;
do {

   // retValue = retValue || <newcondition>;
   retValue |= <newcondition>;


} while( !retValue );
return retValue;