java - 如何在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
解决方案
最直接的方法之一是编写一个(私有)方法来对整个数组进行检查。就像是:
/** 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
}
推荐阅读
- android - Android:如何在 DateRangePicker 中设置一周的第一天?
- css - 如何在 xaringan 演示文稿中隐藏代码块?
- python - 如何添加其他文本字符串以在数据框的特定行上打印
- reactjs - 当从父级传递的道具和状态发生变化时,如何使组件重新渲染?
- kotlin - 如何返回 Kotlin/本机字符串?
- c# - 如何wpf画布背景连续更改图像
- java - 跳轨方式
- ruby-on-rails - 如何为 rspec 中的创建和更新操作编写测试用例?
- r - 在光栅图像中将双月儒略日转换为日期
- c# - ASP.NET:使用#temp 表存储会话以在其上运行其他存储过程