构建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()` 方法来模拟电梯的运行,直到所有电梯都没有任务为止。您可以根据需要添加更多的功能,例如等待时间和错误检查。

相关推荐

最新推荐

recommend-type

基于电力电子变压器并联运行动态的Matlab仿真设计

本文基于有功和无功调差特性方程建立了PET控制策略及模型,基于该模型对PET并联运行动态过程进行仿真研究。仿真结果表明,该控制策略可以在保持额定供电频率的前提下,实现有功、 无功负荷的稳定分配,且动态特性...
recommend-type

交错并联CCM Boost PFC变换器研究

在采用占空比补偿电流控制策略后,两支路电流均为3.6 A,两个MOS管的电流峰值均为6.8 A,均流效果明显,开关管的电流应力减小,验证了占空比补偿电流控制交错并联CCM Boost PFC变换器的可行性。
recommend-type

多VSG并联组网下的功率分配策略研究

基于孤岛微电网下的VSG虚拟阻抗的双闭环控制策略,在外环中引入励磁调节器,考虑实际导线参数,提出一种多VSG并联组网下的功率分配策略,通过搭建两台不同容量的VSG并联系统仿真模型,实现VSG在并网下按照额定容量比...
recommend-type

基于TL431的并联扩流稳压电路的设计方案

本文根据TL431三端可调精密内部结构及特点,阐述了并联稳压电路和串联稳压电路的基本构成和性能,提出了一种TL431的线性精密稳压电源的设计方案。
recommend-type

电源技术中的基于TL431的并联扩流稳压电路的设计方案

导读:本文根据TL431三端可调精密内部结构及特点,阐述了并联稳压电路和串联稳压电路的基本构成和性能,提出了一种TL431的线性精密稳压电源的设计方案。  1.引言  TL431是一个有良好热稳定性能的三端可调精密...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。