0

我想在这样的条件下添加系列

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;
    }

有没有最好的方法来做到这一点?

4

6 回答 6

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;
于 2013-02-09T10:22:42.907 回答
0

尝试使用 switch case 来解决您的问题

于 2013-02-09T10:21:43.990 回答
0

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

list.contains(value)

于 2013-02-09T10:23:04.377 回答
0
if (count <=1089 && count%33==0){
   return true;
else
 return false;
于 2013-02-09T10:25:02.620 回答
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");
            }
于 2013-02-09T10:32:53.940 回答
0

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

retValue = false;
do {

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


} while( !retValue );
return retValue;
于 2013-02-09T10:43:10.253 回答