首页 > 解决方案 > 如何在while语句中表示范围?

问题描述

我正在编写一个有趣的小游戏,它为多个队友使用动作系统。我不知道如何在while循环中表示数组中所有成员0-9的范围。

我知道,有一种方法可以关闭打开,但我不知道它如何适合代码。

int[2][10][0] hea //Using two teams, each with 10 members, who have multiple traits
// ^ I know this isn't perfect syntax 

while (hea[0][0-9][0]!=0){ // Tests for if at least one member of team has actions
    Actions
}
// Is there a way to represent the middle step in the array without typing out all and statements

标签: javamultidimensional-arraywhile-loop

解决方案


最直接的方法之一是编写一个(私有)方法来对整个数组进行检查。就像是:

/** Tests if at least one member of the team has actions left */
private boolean haveActionsLeft(int [][][] hea, int team, int members) {
  for (int m = 0; m < members; m++) {
    if (hea[team][m][0] >= 0) {
      return true;
    }
  }
  return false;
}

然后您可以将该函数调用到您的 while 语句的条件中:

...
while (haveActionsLeft(hea, 0, 10)) {
  Actions
}

推荐阅读