首页 > 解决方案 > 如何检查循环结束,三轮检查java

问题描述

我是 java 新手,正在尝试为解析的数据构建游戏流程。

  1. 游戏是每回合3次,所以如果ArrayList中的场景比较多,游戏会提示用户继续玩
  2. 如果只有三个场景,游戏直接结束,不提示用户继续。
  3. 如果场景数不除以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);
            
        }
    }
}

标签: java

解决方案


我认为你已经使逻辑比它需要的更复杂。您正在尝试处理每种情况,就好像它恰好属于三种不同的特殊情况之一:

  • 特殊情况 #1如果它是列表中的最后一个场景:
    • 播放场景
    • 打印统计数据
  • 特殊情况 #2如果它是每第三个场景,但不是列表中的最后一个:
    • 播放场景
    • 打印统计数据
    • 询问“继续播放?”,如果用户想停止则中断循环
  • 特殊情况 #3如果既不是每三种情况,也不是列表中的最后一种情况:
    • 播放场景

主要问题是尝试单独测试每个特殊情况变得混乱且容易出错。但此外,您最终不必要地复制了“播放场景”和“打印统计信息”代码;如果需要更改任何代码,这会导致维护方面的麻烦,因为您现在必须记住在多个位置进行更改。

但是这里有一些机会可以精简和简化逻辑,并消除重复代码。我看到它更像这样:

  • 每种情况的正常情况(因此无需特殊测试):
    • 播放场景
  • 特殊情况 #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;
            } 
        }
    }
}

我冒昧地将循环重写为 from i=0to i<scenarios.size(),并直接使用ito 索引scenarios,因此现在可以测试完整回合的第三个场景i%3==2(i+1)%3==0如果您愿意,也可以使用)。可应要求提供我选择的冗长理由。


推荐阅读