如何检查循环结束,三轮检查java
我是java新手,正在尝试为解析的数据构建游戏流程
- 游戏每轮三次,因此如果ArrayList中有更多场景,游戏将提示用户继续玩
- 如果只有三种情况,游戏将直接结束,而不提示用户继续李>
- 如果场景数不除以3。例如,10,游戏没有场景,直接结束游戏李>
任何帮助或暗示都将不胜感激。谢谢!
我试过这样做:
public void interactConfig (ArrayList<Scenario> scenarios, Audit audit) throws IOException {
ArrayList<Character> passengers = new ArrayList<Character>(); // create new reference arrayList
ArrayList<Character> pedestrians = new ArrayList<Character>(); // otherwise, it will be all the data
for (int i = 1; i < scenarios.size() + 1; i++) {
Scenario s = scenarios.get(i-1);
passengers = s.getPassengers();
pedestrians = s.getPedestrians();
System.out.println(s.toString());
audit.addRun();
System.out.println("Who should be saved? (passenger(s) [1] or pedestrian(s) [2])");
String command = in.nextLine();
// want to check if the game reaches the end
// or run out of the scenarios, but failed
if (i == scenarios.size() + 1) {
decisionCalculate(command, audit, passengers, pedestrians);
System.out.println(audit.toString());
audit.printStatistic();
}
// three scenarios per round is perfect
if (i != 0 && i % 3 == 0) {
decisionCalculate(command, audit, passengers, pedestrians);
System.out.println(audit.toString());
audit.printStatistic();
System.out.println("Would you like to continue? (yes/no)");
playAgain = in.nextLine();
if (playAgain.equals("yes"))
continue;
else
break;
}
// if the numbers of scenarios is not yet three times, the game keeps going
else if (i == 0 && (i % 3) != 0) {
decisionCalculate(command, audit, passengers, pedestrians);
}
}
}
# 1 楼答案
如果使用
i % 3 == 0
,它将显示第一轮的四个场景(0、1、2、3)。这就是我为什么设定从1开始因此,为了降低复杂性并提高可读性,我选择使用另一个名为
numScenario
的变量来表示场景的实际数量,并返回int i = 0
。然后,使用模来进行流量控制就更容易了我喜欢这样: