在if条件java中添加系列
我想在这样的条件下添加系列在if条件java中添加系列,java,if-statement,Java,If Statement,我想在这样的条件下添加系列 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
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;
}
有什么最好的方法可以做到这一点吗?尝试使用switch case来解决您的问题因此,基本上您是在通过
33
检查可除性,然后使用模运算符*:
if (count % 33 == 0) {
retValue = true;
} else {
retValue = false;
}
如果要确认该范围,可以将其添加到条件中:
if (count >= 33 && count <= 1089 && count % 33 == 0)
使用ArrayList
,用整数值填充它。然后简单地说:
list.contains(value)
如果(countlist list list=new ArrayList(){33,66,99,132,165,1198};//将您的数字添加到此列表中,可以在程序的其他部分中重用
如果(列表包含(计数))
{
retValue=true;
系统输出ptinrtln(“找到的元素”);
}
其他的
{
retValue=false;
System.out.ptinrtln(“未找到元素”);
}
如前所述,您可以检查%33
,但如果确实需要检查任意条件的列表,您可以迭代创建答案
retValue = false;
do {
// retValue = retValue || <newcondition>;
retValue |= <newcondition>;
} while( !retValue );
return retValue;
retValue=false;
做{
//retValue=retValue | |;
retValue |=;
}而(!retValue);
返回值;
小细节:他没有在它的条件中包含0。用语法错误替换:0
是数学,不是编程。@MartijnCourteaux..是的,愚蠢的错误。处理好了。:)非常感谢,你帮助了我:)
if (count <=1089 && count%33==0){
return true;
else
return false;
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");
}
retValue = false;
do {
// retValue = retValue || <newcondition>;
retValue |= <newcondition>;
} while( !retValue );
return retValue;