Java实现两户电梯运行(半成品)
2021/11/12 17:12:10
本文主要是介绍Java实现两户电梯运行(半成品),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目的与结果:
居住的楼层是一层二户电梯,每次上下电梯我就在思量这电梯的具体运行逻辑,以及两台电梯如何交互实现运行的。就想着闲了没事稍微写出来一个类似的,就趁着最近项目上线前主要是测试bug,修改bug,挺清闲的就开始着手设计,果然设想是独立别墅,成品是茅草土屋,开始还干劲十足,后面因为停了一个星期,再看自己写的代码,纵然都有注释,还是食之无味,没了开始的干劲,最后模拟电梯运行也是草草结束。
这个示例就是一个控制台的小demo,虽然自己报着有始有终的态度粗糙完成,但是选择了指定电梯,电梯上行到达指定的楼层与电梯下行还是有待优化。比如:当前4层要下楼,6层也要下楼,9层也要下楼,电梯该如何进行,这些在设计实体类的时候我是有考虑的,也涉及了,但是自己实在是懒得操作了,有待有兴趣的小伙伴可以完善。
需求讲解
- 两步电梯,共用上行,下行按钮。
- 电梯运行基本原则是同等条件下右侧电梯优先运行。
- 两步电梯具体的运行的可能组合由于随便写在A4纸上,也丢掉了,具体的大家可以观察生活中的二步电梯组合的楼层,自己分析一下。
- 通过分析可得 每步电梯都要有:运行方向,运行状态,内部开关,目标楼层,当前楼层,且都是全局共享的;每层用户都有:上行按钮,下行按钮,所在楼层数。(ps: 本人懒省事定义到一个实体中)
- 电梯每到一层目标楼层,会有短暂的“休息”时间,门关上后一秒进入“运行”状态,此时再点击上行,下行按钮则无法开门。
- 每步电梯有最高层,最低层原则,即当一步电梯运行的方向相同时,更高层,或更底层有需求,则优先到达最高层,或最底层。(ps: 坐过电梯观察过得都懂吧)
- 电梯内居民选择指定楼层要开门,电梯外用户点击上行或下行按钮若与正在运行电梯方向一致,也应开门。(疑问:有过进了电梯要上行,结果先到了负一层的经历吧)
- 有兴趣的再思考思考。
附带自己的半成品
自己只写了三个类:
RunLift.java 是实体类,定义了上述分析的参数字段;RunLiftMethod.java 是方法类,封装了所有编写的方法;
Running.java 是运行类,负责调用方法运行。
有兴趣的小伙伴来完善这个可怜的半成品QAQ。
RunLift.class
package com.soft.TwoOfLift; import java.util.HashMap; import java.util.List; import java.util.Scanner; public class RunLift { private int floorMy; //自己所在楼层 private static HashMap<Integer,Integer> downOrUp = new HashMap<>(); //上或下 参数一楼层数,参数二,上下楼梯按钮 1上 2 下 private static List<Integer> floorTargetLift; //准备到几楼 左 private static List<Integer> floorTargetRight; // 到几楼, 右 private static int floorNumRight; //当前右侧电梯所在楼层 private static int floorNumLeft; //当前左侧电梯所在楼层 private static int switchLeft; //电梯内部开关左 1 开 2 关 private static int switchRight; //电梯内部开关右 1 开 2 关 private static int runFlagRight; // 0 未运行 1 运行 private static int runFlagLeft; // 0 未运行 1 运行 private static int runLeftDirection ; //当前左侧电梯运行方向 1上 2 下 private static int runRightDirection; //当前右侧电梯运行方向 1上 2下 //对各楼层按钮赋初值,默认20层 { for (int i = 0; i < 20; i++) { downOrUp.put(i+1,0); } floorNumLeft = 1; floorNumRight = 1; } public int getFloorMy() { return floorMy; } public void setFloorMy(int floorMy) { this.floorMy = floorMy; } public static HashMap<Integer, Integer> getDownOrUp() { return downOrUp; } public static void setDownOrUp(HashMap<Integer, Integer> downOrUp) { RunLift.downOrUp = downOrUp; } public static List<Integer> getFloorTargetLift() { return floorTargetLift; } public static void setFloorTargetLift(List<Integer> floorTargetLift) { RunLift.floorTargetLift = floorTargetLift; } public static List<Integer> getFloorTargetRight() { return floorTargetRight; } public static void setFloorTargetRight(List<Integer> floorTargetRight) { RunLift.floorTargetRight = floorTargetRight; } public static int getFloorNumRight() { return floorNumRight; } public static void setFloorNumRight(int floorNumRight) { RunLift.floorNumRight = floorNumRight; } public static int getFloorNumLeft() { return floorNumLeft; } public static void setFloorNumLeft(int floorNumLeft) { RunLift.floorNumLeft = floorNumLeft; } public static int getSwitchLeft() { return switchLeft; } public static void setSwitchLeft(int switchLeft) { RunLift.switchLeft = switchLeft; } public static int getSwitchRight() { return switchRight; } public static void setSwitchRight(int switchRight) { RunLift.switchRight = switchRight; } public static int getRunFlagRight() { return runFlagRight; } public static void setRunFlagRight(int runFlagRight) { RunLift.runFlagRight = runFlagRight; } public static int getRunFlagLeft() { return runFlagLeft; } public static void setRunFlagLeft(int runFlagLeft) { RunLift.runFlagLeft = runFlagLeft; } public static int getRunLeftDirection() { return runLeftDirection; } public static void setRunLeftDirection(int runLeftDirection) { RunLift.runLeftDirection = runLeftDirection; } public static int getRunRightDirection() { return runRightDirection; } public static void setRunRightDirection(int runRightDirection) { RunLift.runRightDirection = runRightDirection; } }
RunLiftMethod.java
package com.soft.TwoOfLift; import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport; import org.junit.runner.notification.RunListener; import java.util.Iterator; import java.util.Scanner; import java.util.Set; public class RunLiftMethod { private Scanner scanner = new Scanner(System.in) ; /** * 上下楼层选择 */ public void InputNum(RunLift runLift){ int num = 0; boolean flag = true; while (flag){ try{ System.out.println("请选择:1上 ,2下"); num = scanner.nextInt(); if (num == 1 || num == 2){ flag = false; //设置要到达的楼层数 RunLift.getDownOrUp().put(runLift.getFloorMy(),num); System.out.println("当前电梯所在层数:"+"左侧:"+RunLift.getFloorNumLeft() + "层 右侧:"+RunLift.getFloorNumRight()+"层"); System.out.println("当前所在楼层为:"+ runLift.getFloorMy()+"层"); }else { System.out.println("请输入 1 或者 2 "); continue; } }catch (Exception e){ System.out.println("请输入整数!"); scanner.next(); } } } //输入到达的楼层 public int scannerTargetFloor(RunLift runLift,int target) { int num = 0; boolean flag = true; while (flag){ try{ System.out.println("请选择要到达的楼层号:"); num = scanner.nextInt(); if (num >= 1 || num <= 20){ flag = false; if (target == 1){ //1 为左侧 ,否则为右侧 RunLift.getFloorTargetLift().add(num); if (num > runLift.getFloorMy()){ RunLift.setRunLeftDirection(1); }else { RunLift.setRunLeftDirection(2); } }else { RunLift.getFloorTargetRight().add(num); if (num > runLift.getFloorMy()){ RunLift.setRunRightDirection(1); }else { RunLift.setRunRightDirection(2); } } System.out.println("当前电梯所在层数:"+"左侧:"+RunLift.getFloorNumLeft() + "层 右侧:"+RunLift.getFloorNumRight()+"层"); System.out.println("当前所在楼层为:"+ runLift.getFloorMy()+"层"); return num; }else { System.out.println("请输入 1--20的整数! "); continue; } }catch (Exception e){ System.out.println("请输入整数!"); scanner.next(); } } return 0; } /** * 到达指定楼层 * @param runLift */ public void toTargetFloor(RunLift runLift,int target) throws InterruptedException { //目标楼层 int targetLift = 0; //获得各层电梯距离目标楼层的高度, int[] arr = this.toTargetNum(runLift.getFloorMy()); int num = 0; //当前所在楼层 int floorMy = runLift.getFloorMy(); if (target == 1 ){//调用左侧电梯 int targetNum = RunLift.getFloorTargetLift().get(0); if (RunLift.getRunLeftDirection()==2){//若为下楼 num = arr[0]; for (int i = floorMy; i >= targetNum; i--) { RunLift.setFloorNumLeft(i); if (targetNum == i) { System.out.println("左侧电梯已到达:" + targetNum + "层。"); RunLift.setSwitchLeft(1); System.out.println(targetNum + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchLeft(2); System.out.println("电梯门关闭"); RunLift.setRunFlagLeft(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(targetNum, 0); //声明该层上下楼参数为0, } else { System.out.println("左侧电梯正在运行,当前位置为:" + RunLift.getFloorNumLeft() + "层。"); RunLift.setSwitchLeft(2); } //有其他人本来在电梯中且要下到自己的楼层 innerUerCheck(num, runLift,1); //其他楼层人同样点击上或下,判断操作。 outUserCheck(num, runLift,1); } }else{ num = arr[0]; for (int i = floorMy; i <= targetNum; i++) { RunLift.setFloorNumLeft(i); if (targetNum == i) { System.out.println("左侧电梯已到达:" + targetNum + "层。"); RunLift.setSwitchLeft(1); System.out.println(num + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchLeft(2); System.out.println("电梯门关闭"); RunLift.setRunFlagLeft(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(targetNum, 0); //声明该层上下楼参数为0, } else { System.out.println("右侧电梯正在运行,当前位置为:" + RunLift.getFloorNumRight() + "层。"); RunLift.setSwitchRight(2); } //有其他人本来在电梯中且要下到自己的楼层 innerUerCheck(num, runLift,1); //其他楼层人同样点击上或下,判断操作。 outUserCheck(num, runLift,1); } } }else { //调用右侧电梯 int targetNum = RunLift.getFloorTargetRight().get(0); if (RunLift.getRunRightDirection()==2){//若为下楼 num = arr[1]; for (int i = floorMy; i >= targetNum; i--) { RunLift.setFloorNumRight(i); if (targetNum == i) { System.out.println("右侧电梯已到达:" + targetNum + "层。"); RunLift.setSwitchRight(1); System.out.println(targetNum + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchRight(2); System.out.println("电梯门关闭"); RunLift.setRunFlagRight(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(targetNum, 0); //声明该层上下楼参数为0, } else { System.out.println("右侧电梯正在运行,当前位置为:" + RunLift.getFloorNumRight() + "层。"); RunLift.setSwitchRight(2); } //有其他人本来在电梯中且要下到自己的楼层 innerUerCheck(num, runLift,1); //其他楼层人同样点击上或下,判断操作。 outUserCheck(num, runLift,1); } }else{ num = arr[1]; for (int i = floorMy; i <= targetNum; i++) { RunLift.setFloorNumRight(i); if (targetNum == i) { System.out.println("右侧电梯已到达:" + targetNum + "层。"); RunLift.setSwitchRight(1); System.out.println(num + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchRight(2); System.out.println("电梯门关闭"); RunLift.setRunFlagRight(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(num, 0); //声明该层上下楼参数为0, } else { System.out.println("右侧电梯正在运行,当前位置为:" + RunLift.getFloorNumRight() + "层。"); RunLift.setSwitchRight(2); } //有其他人本来在电梯中且要下到自己的楼层 innerUerCheck(num, runLift,1); //其他楼层人同样点击上或下,判断操作。 outUserCheck(num, runLift,1); } } } } /** * 选择运行的电梯与到达指定楼层 * @param runLift * @return */ public int openAndWait(RunLift runLift) throws InterruptedException { //判断电梯所在楼层数与用户所在楼层的位置判断电梯启动 1 左侧 2 右侧 int checkfar = checkFar(runLift); int floorMy = runLift.getFloorMy(); int floorNumLeft = RunLift.getFloorNumLeft(); int floorNumRight = RunLift.getFloorNumRight(); if (checkfar == 1 ) { //用户高于左侧电梯楼层 if (floorMy > floorNumLeft) { //设置电梯为上行 RunLift.setRunLeftDirection(1); //声明电梯为运行中 RunLift.setRunFlagLeft(1); for (int num = floorNumLeft; num <= floorMy; num++) { RunLift.setFloorNumLeft(num); if (num == floorMy) { System.out.println("左侧电梯已到达您的楼层:" + floorMy + "层。"); RunLift.setSwitchLeft(1); System.out.println(num + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchLeft(2); System.out.println("电梯门关闭"); RunLift.setRunFlagLeft(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(num, 0); //声明该层上下楼参数为0, } else { System.out.println("左侧电梯正在运行,当前位置为:" + RunLift.getFloorNumLeft() + "层。"); RunLift.setSwitchLeft(2); } //有其他人本来在电梯中且要下到自己的楼层 innerUerCheck(num, runLift,checkfar); //其他楼层人同样点击上或下,判断操作。 outUserCheck(num, runLift,checkfar); } } //当前用户楼层小于电梯所在楼层 else if (floorMy < floorNumLeft) { //设置电梯为下行 RunLift.setRunLeftDirection(2); //声明电梯为运行中 RunLift.setRunFlagLeft(1); for (int num = floorNumLeft; num >= floorMy; num--) { RunLift.setFloorNumLeft(num); if (num == floorMy) { System.out.println("左侧电梯已到达您的楼层:" + floorMy + "层。"); RunLift.setSwitchLeft(1); System.out.println(num + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchLeft(2); System.out.println("电梯门关闭"); RunLift.setRunFlagLeft(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(num, 0); //声明该层上下楼参数为0, } else { System.out.println("左侧电梯正在运行,当前位置为:" + RunLift.getFloorNumLeft() + "层。"); RunLift.setSwitchLeft(2); } //有其他人本来在电梯中且要下到自己的楼层 innerUerCheck(num, runLift,checkfar); //其他楼层人同样点击上或下,判断操作。 outUserCheck(num, runLift,checkfar); } }else{ System.out.println("左侧电梯已到达您的楼层:" + floorMy + "层。"); RunLift.setSwitchLeft(1); System.out.println(floorMy + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchLeft(2); System.out.println("电梯门关闭"); RunLift.setRunFlagLeft(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(floorMy, 0); //声明该层上下楼参数为0, } }else if(checkfar == 2){ //用户高于右侧电梯楼层 if (floorMy > floorNumRight) { //设置电梯为上行 RunLift.setRunRightDirection(1); //声明电梯为运行中 RunLift.setRunFlagRight(1); for (int num = floorNumRight; num <= floorMy; num++) { RunLift.setFloorNumRight(num); if (num == floorMy) { System.out.println("右侧电梯已到达您的楼层:" + floorMy + "层。"); RunLift.setSwitchRight(1); System.out.println(num + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchRight(2); System.out.println("电梯门关闭"); RunLift.setRunFlagRight(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(num, 0); //声明该层上下楼参数为0, } else { System.out.println("右侧电梯正在运行,当前位置为:" + RunLift.getFloorNumRight() + "层。"); RunLift.setSwitchRight(2); } //有其他人本来在电梯中且要下到自己的楼层 innerUerCheck(num, runLift,checkfar); //其他楼层人同样点击上或下,判断操作。 outUserCheck(num, runLift,checkfar); } } //当前用户楼层小于电梯所在楼层 else if (floorMy < floorNumRight) { //设置电梯为下行 RunLift.setRunRightDirection(2); //声明电梯为运行中 RunLift.setRunFlagRight(1); for (int num = floorNumRight; num >= floorMy; num--) { RunLift.setFloorNumRight(num); if (num == floorMy) { System.out.println("右侧电梯已到达您的楼层:" + floorMy + "层。"); RunLift.setSwitchRight(1); System.out.println(num + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchRight(2); System.out.println("电梯门关闭"); RunLift.setRunFlagRight(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(num, 0); //声明该层上下楼参数为0, } else { System.out.println("右侧电梯正在运行,当前位置为:" + RunLift.getFloorNumRight() + "层。"); RunLift.setSwitchRight(2); } //有其他人本来在电梯中且要下到自己的楼层 innerUerCheck(num, runLift,checkfar); //其他楼层人同样点击上或下,判断操作。 outUserCheck(num, runLift,checkfar); } }else{ System.out.println("右侧电梯已到达您的楼层:" + floorMy + "层。"); RunLift.setSwitchRight(1); System.out.println(floorMy + "层已经到了,电梯门打开。"); Thread.sleep(2000); RunLift.setSwitchRight(2); System.out.println("电梯门关闭"); RunLift.setRunFlagRight(0);//声明电梯为运行结束 RunLift.getDownOrUp().put(floorMy, 0); //声明该层上下楼参数为0, } } return checkfar; } /** * 判断优先指派哪个电梯,1左侧, 2 右侧 * @param runLift * @return */ public int checkFar(RunLift runLift) { int liftNum = 0; int floorMy = runLift.getFloorMy(); int downOrUp = runLift.getDownOrUp().get(runLift.getFloorMy()); //两个电梯都闲置 if (RunLift.getRunFlagLeft() == 0 && RunLift.getRunFlagRight() == 0){ liftNum = allFree(floorMy, downOrUp); } //左侧运行,右侧闲置 if (RunLift.getRunFlagLeft() == 1 && RunLift.getRunFlagRight() == 0){ liftNum = LeftRun(floorMy, downOrUp); } //右侧运行, 左侧闲置 if (RunLift.getRunFlagLeft() == 1 && RunLift.getRunFlagRight() == 0){ liftNum = RightRun(floorMy, downOrUp); } return liftNum; } /** * 左侧闲置,右侧运行 * @param floorMy * @param downOrUp * @return */ public int RightRun(int floorMy, int downOrUp) { //获取楼层差 int[] floorHigh = floorNum(floorMy); //电梯方向 与 用户方向一致 上 if (RunLift.getRunRightDirection() == 1 && downOrUp == 1 ){ //右侧电梯楼层高于用户楼层 if (RunLift.getFloorNumRight() >floorMy){ return 1; } //左侧电梯楼层低于用户楼层 else{ //右侧楼层差高于左侧 if (floorHigh[0] < floorHigh[1]){ return 1; }else { return 2; } } //方向一致都是下 }else if (RunLift.getRunRightDirection() == 2 && downOrUp == 2 ){ //右侧电梯楼层高于用户楼层 if (RunLift.getFloorNumLeft() >floorMy){ return 1; } //左侧电梯楼层低于用户楼层 else{ //右侧楼层差高于左侧 if (floorHigh[0] < floorHigh[1]){ return 1; }else { return 2; } } //方向不一致 右侧上 用户下 }else if(RunLift.getRunRightDirection() == 1 && downOrUp == 2 ){ return 1; //左侧下 用户上 }else if (RunLift.getRunRightDirection() == 2 && downOrUp == 1 ){ return 1; } return 1; } /** * 左侧运行,右侧闲置 * @param floorMy * @param downOrUp * @return */ public int LeftRun(int floorMy, int downOrUp) { //获取楼层差 int[] floorHigh = floorNum(floorMy); //电梯方向 与 用户方向一致 上 if (RunLift.getRunLeftDirection() == 1 && downOrUp == 1 ){ //左侧电梯楼层高于用户楼层 if (RunLift.getFloorNumLeft() >floorMy){ return 2; } //左侧电梯楼层低于用户楼层 else{ //左侧楼层差高于右侧 if (floorHigh[0] > floorHigh[1]){ return 2; }else { return 1; } } //方向一致都是下 }else if (RunLift.getRunLeftDirection() == 2 && downOrUp == 2 ){ //左侧电梯楼层高于用户楼层 if (RunLift.getFloorNumLeft() >floorMy){ return 2; } //左侧电梯楼层低于用户楼层 else{ //左侧楼层差高于右侧 if (floorHigh[0] > floorHigh[1]){ return 2; }else { return 1; } } //方向不一致 左侧上 用户下 }else if(RunLift.getRunLeftDirection() == 1 && downOrUp == 2 ){ return 2; //左侧下 用户上 }else if (RunLift.getRunLeftDirection() == 2 && downOrUp == 1 ){ return 2; } return 2; } /** * 两台全修改的情况 * @param floorMy * @param downOrUp * @return 2 右侧 1 左侧 */ public int allFree(int floorMy, int downOrUp){ int[] floorHigh = floorNum(floorMy); if (RunLift.getRunFlagRight() == RunLift.getRunFlagLeft()){ return 2; } if (floorHigh[1] >floorHigh[0]){ return 1; }else{ return 2; } } /** * 两个电梯与用户楼层 的楼层差 * @param floorMy * @return */ public int[] floorNum(int floorMy){ int floorNumRight = RunLift.getFloorNumRight(); int floorNumLeft = RunLift.getFloorNumLeft(); //右侧距离目标层数 int rightNum = Math.abs(floorNumRight - floorMy); //左侧........... int leftNum = Math.abs(floorNumLeft - floorMy); int[] arr = new int[2]; arr[0] = leftNum; arr[1] = rightNum; return arr; } /** * 两个电梯与目标楼层 的楼层差 * @param floorMy * @return */ public int[] toTargetNum(int floorMy){ Integer targetLift = 0; Integer targetRight = 0; if (RunLift.getFloorTargetLift().size()!=0){ targetLift = RunLift.getFloorTargetLift().get(0); } if (RunLift.getFloorTargetRight().size()!=0){ targetRight = RunLift.getFloorTargetRight().get(0); } //右侧 int rightNum = Math.abs(targetRight - floorMy); //左侧........... int leftNum = Math.abs(targetLift - floorMy); int[] arr = new int[2]; arr[0] = leftNum; arr[1] = rightNum; return arr; } /** * 电梯外用户按上下按钮开门,关门操作 * @param num */ public void outUserCheck(int num, RunLift runLift,int checkFar) throws InterruptedException { if (checkFar == 1){ //其他楼层人同样点击上或下。 Set<Integer> keys = RunLift.getDownOrUp().keySet(); Iterator<Integer> iterator = keys.iterator(); while (iterator.hasNext()){ //楼层在范围之内,且状态不为0 Integer next = iterator.next(); if (num ==next && RunLift.getDownOrUp().get(next)!=0){ //如果自己,其他楼层,楼层内用户,目的楼层一致,跳过操作 if (runLift.getFloorMy() == num){ continue; } //如果其他楼层,楼层内用户,目的楼层一致,跳过操作 for (int i = 0; i <RunLift.getFloorTargetLift().size(); i++) { if (RunLift.getFloorTargetLift().get(i) == num){ continue; } } //开门 RunLift.setSwitchLeft(1); System.out.println(num +"层已经到了,电梯门打开。"); Thread.sleep(2000); //关门并清楚上下楼按钮 RunLift.setSwitchLeft(2); System.out.println("电梯门关闭。"); RunLift.getDownOrUp().put(iterator.next(),0); } } }else { //其他楼层人同样点击上或下。 Set<Integer> keys = RunLift.getDownOrUp().keySet(); Iterator<Integer> iterator = keys.iterator(); while (iterator.hasNext()){ //楼层在范围之内,且状态不为0 Integer next = iterator.next(); if (num ==next && RunLift.getDownOrUp().get(next)!=0){ //如果自己,其他楼层,楼层内用户,目的楼层一致,跳过操作 if (runLift.getFloorMy() == num){ continue; } //如果其他楼层,楼层内用户,目的楼层一致,跳过操作 for (int i = 0; i <RunLift.getFloorTargetRight().size(); i++) { if (RunLift.getFloorTargetRight().get(i) == num){ continue; } } //开门 RunLift.setSwitchRight(1); System.out.println(num +"层已经到了,电梯门打开。"); Thread.sleep(2000); //关门并清楚上下楼按钮 RunLift.setSwitchRight(2); System.out.println("电梯门关闭。"); RunLift.getDownOrUp().put(iterator.next(),0); } } } } /** * 电梯内用户到指定楼层的操作 * @param num * @throws InterruptedException */ public void innerUerCheck(int num,RunLift runLift,int checkFar) throws InterruptedException { if (checkFar == 1){ for (int i = 0; i <RunLift.getFloorTargetLift().size(); i++) { if (RunLift.getFloorTargetLift().get(i) ==num){ //如果自己,其他楼层,楼层内用户,目的楼层一致,跳过操作 if (runLift.getFloorMy() == num){ continue; } //开门 RunLift.setSwitchLeft(1); System.out.println(num +"层已经到了,电梯门打开。"); Thread.sleep(2000); //关门并从列表移除楼层 RunLift.setSwitchLeft(2); RunLift.getFloorTargetLift().remove(i); System.out.println("电梯门关闭"); } } }else { for (int i = 0; i <RunLift.getFloorTargetRight().size(); i++) { if (RunLift.getFloorTargetRight().get(i) == num){ //如果自己,其他楼层,楼层内用户,目的楼层一致,跳过操作 if (runLift.getFloorMy() == num){ continue; } //开门 RunLift.setSwitchRight(1); System.out.println(num +"层已经到了,电梯门打开。"); Thread.sleep(2000); //关门并从列表移除楼层 RunLift.setSwitchRight(2); RunLift.getFloorTargetRight().remove(i); System.out.println("电梯门关闭"); } } } } /** * 判断是否重新来过 * @return */ public int checkNew() { int num = 0; boolean flag = true; while (flag){ try{ System.out.println("是否继续兜风?请选择:1是 ,2否"); num = scanner.nextInt(); if (num == 1 || num == 2){ flag = false; return num; }else { System.out.println("请输入 1 或者 2 "); continue; } }catch (Exception e){ System.out.println("请输入整数!"); scanner.next(); } } return 1; } }
Running.java
package com.soft.TwoOfLift; import java.util.ArrayList; import java.util.List; public class Running { public static void main(String[] args) throws InterruptedException { while (true){ //数据初始化 RunLiftMethod runLiftMethod = new RunLiftMethod(); RunLift runLift = new RunLift(); //左侧目标层 List<Integer> listLeft =new ArrayList<Integer>(); //右侧目标层 List<Integer> listRight =new ArrayList<Integer>(); //选择自己所在的楼层 runLift.setFloorMy(5); //初始化 RunLift.setFloorTargetLift(listLeft); RunLift.setFloorTargetRight(listRight); //选择上下楼 runLiftMethod.InputNum(runLift); //等待电梯到达,返回哪侧电梯到达 int target = runLiftMethod.openAndWait(runLift); //输入到达的楼层 runLiftMethod.scannerTargetFloor(runLift,target); //到达指定的楼层,待优化:当前4层要下楼,6层也要下楼,在测方法中优化,未判断floorTargetLift或者floorTargetRight //getDownOrUp字段优化 runLiftMethod.toTargetFloor(runLift,target); //循环判断 if (runLiftMethod.checkNew() == 1){ continue; }else { break; } } } }
这篇关于Java实现两户电梯运行(半成品)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南