首页 > 解决方案 > 我的电梯循环数量做错了什么?

问题描述

这是我的专栏类,我在第二段中调用了我的电梯数量:

class Column {
constructor(floorsNumber, elevatorsNumber) {
    this.floorsNumber = floorsNumber;
    this.elevatorsNumber = elevatorsNumber;
    this.elevatorsList = [];
    this.directionButtonsList = [];
    
    // Number of elevators
    for (let i = 0; i < elevatorsNumber; i++) {
        this.elevatorsList.push(new Elevator(0, floorsNumber));
}



    for (let i = 0; i < this.floorsNumber; i++) {
        //  floor1 has no DOWN direction button
        if (i === 1) {
            this.directionButtonsList.push(new DirectionButton(i, "up", false));
        } 
        // floor10 has no UP direction button  
        else if (i === 10) {
            this.directionButtonsList.push(new DirectionButton(i, "down", false));
            
        }
         // The rest has both directions
        else {
            this.directionButtonsList.push(new DirectionButton(i, "up", false));
            this.directionButtonsList.push(new DirectionButton(i, "down", false));
        }
    }
}

这就是我如何找到最好的电梯:

// Finding the best  elevator to answer the user's request
findElevator(requestedFloor, direction) {  
    var bestGap = this.floorsNumber;
    var bestElevator = null;

    for (let i = 0; i < this.elevatorsList.length; i++) { 
            // If the elevators and request's direction is moving UP and the requested floor is above the elevator
        if (this.elevatorsList[i].direction === "up" && direction === "up" && requestedFloor > this.elevatorsList[i].currentFloor) {
            bestElevator = this.elevatorsList[i];
        }   // If the elevators and request's direction is moving DOWN and the requested floor is under the elevator
        else if (this.elevatorsList[i].direction === "down" && direction === "down" && requestedFloor < this.elevatorsList[i].currentFloor) {
            bestElevator = this.elevatorsList[i]; 
                 //if elevator and request are descending and the request is below the elevator
        }   // If the elevator is idle
        else if (this.elevatorsList[i].status == "idle") {
            bestElevator = this.elevatorsList[i];
        }
        else {
            for (let i = 0; i < this.elevatorsList.length; i++) {
                // Absolute function is needed otherwise minus numbers would happen and the situation would be misinterpret
                let gap = Math.abs(this.elevatorsList[i].currentFloor - requestedFloor);
                if (gap < bestGap) {
                    bestElevator = this.elevatorsList[i];
                    bestGap = gap;   //Select the smallest gap to find the bestElevator
                }
            }
        }
    }
    console.log("Best elevator is found on floor " + bestElevator.currentFloor);
    return bestElevator; 
}

这是我遇到一个小问题的场景,但我不能把手指放在上面的代码中我做错的地方:function Scenario1() {

columntest = new Column(10, 2);

columntest.elevatorsList[0].currentFloor = 2;
columntest.elevatorsList[0].direction = "";
columntest.elevatorsList[0].status = "idle";
columntest.elevatorsList[0].queue = [];

columntest.elevatorsList[1].currentFloor = 5;
columntest.elevatorsList[1].direction = "";
columntest.elevatorsList[1].status = "idle";
columntest.elevatorsList[1].queue = [];

columntest.requestElevator(1, "up");

} 场景 1();`

我的问题是为什么场景不是呼叫我的电梯 [0],而不是 [1]。当0明显比电梯1更近...

标签: javascript

解决方案


据我所知,这是因为这种情况:

 else if (this.elevatorsList[i].status == "idle") {
            bestElevator = this.elevatorsList[i];
        }

对于 for 循环的每次迭代,代码将按顺序运行 if/else 语句。所以在 i==0 时,电梯没有向上移动,因此 if 条件被忽略。电梯没有向下移动,因此忽略 else if 条件。电梯空闲,因此运行 else if 条件,并且 bestElevator 设置为该索引,并且不运行 for 循环比较距离。

我认为如果你想在调用空闲电梯之前检查是否有移动电梯,你必须完全遍历阵列寻找移动电梯,如果没有找到,则运行一个循环比较空闲电梯。


推荐阅读