如何检查循环结束,三轮检查java
我是java新手,正在尝试为解析的数据构建游戏流如何检查循环结束,三轮检查java,java,Java,我是java新手,正在尝试为解析的数据构建游戏流 游戏每轮三次,因此如果ArrayList中有更多场景,游戏将提示用户继续玩 如果只有三个场景,游戏将直接结束,而不提示用户继续 如果场景数不除以3。例如,10,游戏没有场景,直接结束游戏 非常感谢您的任何帮助或提示。谢谢大家! 我是这样尝试的: public void interactConfig (ArrayList<Scenario> scenarios, Audit audit) throws IOException {
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);
}
}
}
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 = 0; i < scenarios.size(); i++) {
int numScenario = i + 1;
Scenario s = scenarios.get(i);
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();
// if it reaches the last round, it will be this scenario
if (numScenario % 3 == 0 && numScenario == scenarios.size()) {
decisionCalculate(command, audit, passengers, pedestrians);
}
// if it's not the end the ArrayList, will be this one
else if (numScenario % 3 == 0 && numScenario != scenarios.size()) {
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;
}
// other scenarios situation
else if (numScenario % 3 != 0) {
decisionCalculate(command, audit, passengers, pedestrians);
}
}
public void interactionconfig(ArrayList场景,Audit Audit)引发IOException{
ArrayList=新建ArrayList();//创建新的引用ArrayList
ArrayList=新建ArrayList();//否则,它将是所有数据
对于(int i=1;i
我想你把逻辑弄得比实际需要的更复杂了。您试图处理每个场景,就好像它恰好属于三种不同的特殊情况中的一种:
- 特殊情况#1如果是列表中的最后一种情况:
- 播放场景
- 打印统计数据
- 特殊情况#2如果是每三种情况,但不是列表中的最后一种情况:
- 播放场景
- 打印统计数据
- 询问“继续播放?”,如果用户想停止,则中断循环
- 特殊情况#3如果不是每三种情况,也不是列表中的最后一种情况:
- 播放场景
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);
}
}
}
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 = 0; i < scenarios.size(); i++) {
int numScenario = i + 1;
Scenario s = scenarios.get(i);
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();
// if it reaches the last round, it will be this scenario
if (numScenario % 3 == 0 && numScenario == scenarios.size()) {
decisionCalculate(command, audit, passengers, pedestrians);
}
// if it's not the end the ArrayList, will be this one
else if (numScenario % 3 == 0 && numScenario != scenarios.size()) {
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;
}
// other scenarios situation
else if (numScenario % 3 != 0) {
decisionCalculate(command, audit, passengers, pedestrians);
}
}
- 正常情况下对于每个场景(因此无需特殊测试):
- 播放场景
- 特例#1如果这是一轮的结束(因为这是该轮的第三个场景或因为这是最后一个场景):
- 打印统计数据
- 特例#1.1如果还有更多的场景(我们知道这是一轮的结束,因此无需再次测试):
- 询问用户“是否继续?”
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 = 0; i < scenarios.size(); i++) {
Scenario s = scenarios.get(i);
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();
decisionCalculate(command, audit, passengers, pedestrians);
if (i == scenarios.size()-1 || i%3==2) // The round is over
System.out.println(audit.toString());
audit.printStatistic();
if (i < scenarios.size()-1)) // More scenarios: keep playing?
{
System.out.println("Would you like to continue? (yes/no)");
playAgain = in.nextLine();
if (playAgain.equalsIgnoreCase("no"))
break;
}
}
}
}
public void interactionconfig(ArrayList场景,Audit Audit)引发IOException{
ArrayList=新建ArrayList();//创建新的引用ArrayList
ArrayList=新建ArrayList();//否则,它将是所有数据
对于(int i=0;i
我已经自由地重写了循环,从
I=0
到I如果使用I%3==0
,它将显示第一轮的四个场景(0、1、2、3)。这就是我为什么设定从1开始
因此,为了降低复杂性并提高可读性,我选择使用另一个名为numcenario
的变量来表示场景的实际数量,并返回int I=0
。然后,更容易使用模来进行流控制
我喜欢这样:
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);
}
}
}
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 = 0; i < scenarios.size(); i++) {
int numScenario = i + 1;
Scenario s = scenarios.get(i);
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();
// if it reaches the last round, it will be this scenario
if (numScenario % 3 == 0 && numScenario == scenarios.size()) {
decisionCalculate(command, audit, passengers, pedestrians);
}
// if it's not the end the ArrayList, will be this one
else if (numScenario % 3 == 0 && numScenario != scenarios.size()) {
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;
}
// other scenarios situation
else if (numScenario % 3 != 0) {
decisionCalculate(command, audit, passengers, pedestrians);
}
}
public void interactionconfig(ArrayList场景,Audit Audit)引发IOException{
阿莱尔