构建3个电梯的电梯组,楼层在10层以上,模拟其运行。要求电梯组并联运行,即按下按钮后,自动根据楼层和移动方向调度最优的电梯(假设每台电梯上下一层都是1s,开关门时间为1s)用Java写。
时间: 2024-02-17 18:01:19 浏览: 19
好的,我可以为您解答这个问题。以下是一个简单的电梯调度系统的Java代码,其中包含了三台电梯的模拟运行。
```java
import java.util.*;
enum Direction {
UP, DOWN, HOLD
}
class Elevator {
private int id;
private int currentFloor;
private Direction direction;
private Set<Integer> destinations;
public Elevator(int id, int currentFloor) {
this.id = id;
this.currentFloor = currentFloor;
this.direction = Direction.HOLD;
this.destinations = new HashSet<>();
}
public int getId() {
return id;
}
public int getCurrentFloor() {
return currentFloor;
}
public Direction getDirection() {
return direction;
}
public Set<Integer> getDestinations() {
return destinations;
}
public void addDestination(int floor) {
destinations.add(floor);
}
public void moveUp() {
currentFloor++;
}
public void moveDown() {
currentFloor--;
}
public void hold() {
direction = Direction.HOLD;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public boolean shouldStop(int floor) {
if (destinations.contains(floor)) {
destinations.remove(floor);
return true;
}
if (direction == Direction.UP && floor > currentFloor) {
return false;
}
if (direction == Direction.DOWN && floor < currentFloor) {
return false;
}
return destinations.isEmpty();
}
}
class ElevatorSystem {
private List<Elevator> elevators;
private int numFloors;
public ElevatorSystem(int numElevators, int numFloors) {
this.numFloors = numFloors;
this.elevators = new ArrayList<>();
for (int i = 0; i < numElevators; i++) {
elevators.add(new Elevator(i + 1, 1));
}
}
public List<Elevator> getElevators() {
return elevators;
}
public int getNumFloors() {
return numFloors;
}
public Elevator requestElevator(int floor, Direction direction) {
Elevator nearest = null;
int minDistance = Integer.MAX_VALUE;
for (Elevator elevator : elevators) {
if (elevator.getDirection() == Direction.HOLD) {
int distance = Math.abs(elevator.getCurrentFloor() - floor);
if (distance < minDistance) {
minDistance = distance;
nearest = elevator;
}
} else if (elevator.getDirection() == direction) {
if (direction == Direction.UP && elevator.getCurrentFloor() < floor) {
int distance = Math.abs(elevator.getCurrentFloor() - floor);
if (distance < minDistance) {
minDistance = distance;
nearest = elevator;
}
} else if (direction == Direction.DOWN && elevator.getCurrentFloor() > floor) {
int distance = Math.abs(elevator.getCurrentFloor() - floor);
if (distance < minDistance) {
minDistance = distance;
nearest = elevator;
}
}
}
}
if (nearest != null) {
nearest.addDestination(floor);
if (nearest.getDirection() == Direction.HOLD) {
if (nearest.getCurrentFloor() < floor) {
nearest.setDirection(Direction.UP);
} else if (nearest.getCurrentFloor() > floor) {
nearest.setDirection(Direction.DOWN);
}
}
}
return nearest;
}
public void step() {
for (Elevator elevator : elevators) {
if (elevator.getDirection() == Direction.UP) {
elevator.moveUp();
if (elevator.shouldStop(elevator.getCurrentFloor())) {
elevator.hold();
} else if (elevator.getCurrentFloor() == numFloors) {
elevator.setDirection(Direction.DOWN);
}
} else if (elevator.getDirection() == Direction.DOWN) {
elevator.moveDown();
if (elevator.shouldStop(elevator.getCurrentFloor())) {
elevator.hold();
} else if (elevator.getCurrentFloor() == 1) {
elevator.setDirection(Direction.UP);
}
} else if (!elevator.getDestinations().isEmpty()) {
int destination = elevator.getDestinations().iterator().next();
if (destination > elevator.getCurrentFloor()) {
elevator.setDirection(Direction.UP);
} else if (destination < elevator.getCurrentFloor()) {
elevator.setDirection(Direction.DOWN);
}
}
}
}
}
public class Main {
public static void main(String[] args) {
ElevatorSystem system = new ElevatorSystem(3, 20);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter floor number (1-20): ");
int floor = scanner.nextInt();
System.out.print("Enter direction (UP or DOWN): ");
Direction direction = Direction.valueOf(scanner.next().toUpperCase());
Elevator elevator = system.requestElevator(floor, direction);
System.out.println("Elevator " + elevator.getId() + " is on its way to floor " + floor);
while (!elevator.getDestinations().isEmpty()) {
system.step();
}
System.out.println("Elevator " + elevator.getId() + " has arrived at floor " + floor);
}
}
}
```
以上代码使用了枚举类型来表示电梯的运行方向和是否停留。系统通过循环调用 `step()` 方法来模拟电梯的运行,直到所有电梯都没有任务为止。您可以根据需要添加更多的功能,例如等待时间和错误检查。