如何在if else语句中表达一系列整数?

时间:2015-02-14 01:12:44

标签: java android if-statement int

目前我的代码

中有if else语句
if (chosenView == findViewById(R.id.playView1) && (4 <= pointCounter && pointCounter <= 9) || (13 <= pointCounter && pointCounter <= 18) || pointCounter >= 22) { }

如果selectedView是playView1且pointCounter等于4,5,6,7,8,9,13,14,15,16,17,18,我只想要运行语句中包含的内容,或22以上的任何东西,但我觉得这是草率的代码,而不是正确的方法来做到这一点。特别是因为当我删除括号时,它给了我一个错误。这是正确的还是有更好的方法来实现这一目标?

2 个答案:

答案 0 :(得分:1)

如果您需要这些特定标准,那么您编写它的方式是最简单和最快速的。 (如果你没有开放式的&#34;高于22&#34;,那么使用Set.contains或数组查找可能会更漂亮但速度会更慢。)

答案 1 :(得分:0)

我不了解Java,但发现了以下页面:

http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/

在我看来,你可以使用类似的东西:

int[] intArray = { 4,5,6,7,8,9,13,14,15,16,17,18 };
if (chosenView == findViewById(R.id.playView1) && (Arrays.asList(intArray).contains(pointCounter) || pointCounter >= 22) { }