因此,我正在制作一个子手游戏,并生成了一个随机单词TheWord,然后使用以下命令对单词中的所有字符进行了排列
char theWordChars[] =t heWord.toCharArray();
然后使用for循环检查按下的键是否等于theWord中的任何字符,并创建了一个名为keyIsFound []的布尔数组:
void keyPressed(){
if(keyCode != 0 || keyCode != UP || keyCode != DOWN || keyCode != LEFT || keyCode != RIGHT){
lastKey = char(keyCode);
}
for (int z = 0; z< theWordChars.length; z++) {
if(lastKey == theWordChars[z]){
keyIsFound[z] = true;
}
}
}
所以现在我要检查的是何时按下了一个按键,但数组keyIsFound中的值没有变化,即按下了一个假字符,然后我可以增加计数器来显示身体部位。我该怎么办?愿意彻底改变它。
最佳答案
通常,您通常使用flag
-通常是boolean
:
// Did we find the key in the word?
boolean found = false;
// Look at all of the characters.
for (int z = 0; z < theWordChars.length; z++) {
// Did they press this one?
if (lastKey == theWordChars[z]) {
// YES! Mark it as found.
keyIsFound[z] = true;
// Remember we found one so we don't add a body part.
found = true;
}
}
if ( !found ) {
// Not found the key they pressed - add a body part.
}
关于java - 处理/java-如何检查数组中的值是否不变,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34162094/