[英]Check if number is in a number list
I am new to programming and I am trying to figure out how to create a list of numbers like so (5, 10, 15, 20, 25, all the way to 500) then be able to check if a player score is equal to any of these numbers. 我是编程新手,我想弄清楚如何创建这样的数字列表(5、10、15、20、25,一直到500),然后能够检查玩家得分是否等于这些数字中的任何一个。
My code so far is this.. It works, but only if the score is 5. 到目前为止,我的代码是这样。.它可以工作,但是仅当分数为5时。
private void drawPower() {
if (myWorld.getScore() == 5) {
batcher.draw(Power, pipe3.getX(), pipe3.getY() + pipe3.getHeight()
+ 30, 20, -14);
}
}
I need to know what needs to be done so instead of 我需要知道需要做什么而不是
if (myWorld.getScore() == 5) {
it would be 这将是
if (myWorld.getScore() == "any number in the list") {
I wouldn't use an array of multiples of 5 from 5 to 100. 我不会使用5到100的5的倍数数组。
I would just check if it's a multiple of 5 and if it's between 5
and 500
. 我只是检查它是否是5的倍数,以及是否在
5
到500
之间。
int score = myWorld.getScore();
if (score >= 5 && score <= 500 && score % 5 == 0) {
@rgettman's answer is definitely the best way. @rgettman的答案肯定是最好的方法。 However to answer the question more generally (as it may come up again):
但是,更一般地回答这个问题(可能会再次出现):
if(Arrays.asList(5, 10, 15, 20).contains(myWorld.getScore()))
However, if the numbers have any sort of pattern to them, a solution like rgettman's is definitely better. 但是,如果数字具有某种形式的模式,那么像rgettman的解决方案肯定更好。 This is if the numbers are more or less arbitrary.
这是数字多少是任意的。
Since the array contains all the numbers from 5 up to 500 (with a step of 5) then: 由于数组包含从5到500的所有数字(以5为步长),因此:
public hasScore(int score) {
int position = (score/5) - 1;
return arrray[position] == score;
}
Ie the score should be in position (i/5) - 1 (since 5 in index 0, 10 in index 1) and so on. 即得分应该在位置(i / 5)-1(因为索引0中为5,索引1中为10),依此类推。
So check if the score is indeed in the expected location it should be 因此,检查分数是否确实在预期位置上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.