[英]What Am I doing wrong with my number of elevators loop?
這是我的專欄 class ,我在第二段中調用了我的電梯數量:
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更近...
據我所知,這是因為這種情況:
else if (this.elevatorsList[i].status == "idle") {
bestElevator = this.elevatorsList[i];
}
對於 for 循環的每次迭代,代碼將按順序運行 if/else 語句。 所以在 i==0 時,電梯沒有向上移動,因此 if 條件被忽略。 電梯沒有向下移動,因此忽略 else if 條件。 電梯空閑,因此運行 else if 條件,並且 bestElevator 設置為該索引,並且不運行 for 循環比較距離。
我認為如果你想在調用空閑電梯之前檢查是否有移動電梯,你必須完全通過陣列來尋找移動電梯,如果沒有找到,那么運行一個循環比較空閑電梯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.