有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何检查循环结束,三轮检查java

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

  1. 游戏每轮三次,因此如果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);
            
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    如果使用i % 3 == 0,它将显示第一轮的四个场景(0、1、2、3)。这就是我为什么设定从1开始

    因此,为了降低复杂性并提高可读性,我选择使用另一个名为numScenario的变量来表示场景的实际数量,并返回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 = 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);
                
            }
            
        }