基于Java的電梯系統(tǒng)實現(xiàn)過程
一、思路
寫一個簡單的電梯系統(tǒng),首先根據(jù)老師提供的需求,寫一下基礎(chǔ)思路:
- 電梯有最高層和最低層,輸入數(shù)字選擇正確樓層數(shù)
- 輸入數(shù)字大于當(dāng)前樓層,則為上行;小于當(dāng)前樓層,則為下行
- 每次輸入數(shù)字的時候,需要對同為上行的數(shù)字或者同為下行的數(shù)字,進(jìn)行排序
- 所輸入的目標(biāo)樓層用集合存放,循環(huán)最低層到最高層,如果當(dāng)前層在集合中存在,顯示開門,若還有目標(biāo)樓層,則關(guān)門,繼續(xù)到下一目標(biāo)樓層。
- 當(dāng)選擇一個目標(biāo)樓層,會生成隨機(jī)重量記錄在目標(biāo)樓層,上行用原來重量加上目標(biāo)樓層重量,下行則用原來重量減去目標(biāo)樓層重量
二、實現(xiàn)
2.1 電梯類
package Ele;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class Elevator {
private List<Integer> upFloorList = new ArrayList<Integer>(); // 上升樓層
private List<Integer> downFloorList = new ArrayList<Integer>(); // 下降樓層
private int[] storeyWeight; // 目標(biāo)層重量
private int capacity; // 電梯最大重量
private int topFloor; // 電梯最高層
private int bottomFloor; // 電梯最底層
private int nowFloor = 1; // 當(dāng)前層
public Elevator(int bottomFloor, int topFloor, int capacity) { //有參構(gòu)造方法
this.topFloor = topFloor;
this.bottomFloor = bottomFloor;
this.capacity = capacity;
// 當(dāng)前樓層減最低層,就是當(dāng)前層重量的下標(biāo) 假如當(dāng)前樓層為5樓,5樓下標(biāo)就是 5-1 = 4
// 初始化目標(biāo)樓層重量,數(shù)組大小 = 最高層 - 最低層 + 1
storeyWeight = new int[(topFloor - bottomFloor + 1)];
}
// 設(shè)置樓層
public void SetFloor(int floorNum) {
//如果 所選樓層 與 所在樓層 相同,則提示
if (floorNum == nowFloor) {
System.out.println("請選擇其它樓層");
return;
}
// 生成90-500之間的隨機(jī)重量
Random random = new Random();
int thisFloorWeight = random.nextInt(500 - 90 + 1) + 90;
int sum = 0;
//目標(biāo)樓層增加的重量
for (int i = 0; i < storeyWeight.length; i++) {
sum += storeyWeight[i];
}
//原重量+增加重量=當(dāng)前重量
System.out.println(floorNum + "層上來重量:" + thisFloorWeight + ",此時總重:" + (sum + thisFloorWeight));
// 如果 目標(biāo)樓層總重量 > 最大重量,提示
if (sum + thisFloorWeight > this.capacity) {
System.out.println("超重了喲");
return;
}
// 當(dāng)前輸入樓層重量加上該樓層新增加重量 后的重量
storeyWeight[floorNum - bottomFloor] += thisFloorWeight;
//如果輸入樓層數(shù) 已經(jīng)在上升或下降樓層的集合中,則只新增重量,不添加樓層
if (!upFloorList.contains(floorNum) && !downFloorList.contains(floorNum)) {
if (floorNum > nowFloor) {
upFloorList.add(floorNum);
// 上升樓層升序排序
Collections.sort(upFloorList);
} else {
downFloorList.add(floorNum);
// 下降樓層降序排序
downFloorList.sort(Collections.reverseOrder());
}
}
}
// 上升:從所在層到所選樓層中的最高層
// 下降:從所在層到所選樓層中的最低層
// 獲得集合中最后一個元素:list.get(list.size()-1);
// 啟動電梯
public void StartElevator() throws InterruptedException {
System.out.println("當(dāng)前第 < " + nowFloor + " > 層");
// 上行
if (upFloorList.size() > 0) {
System.out.println("---電梯上行---");
for (int i = nowFloor + 1; i <= upFloorList.get(upFloorList.size() - 1); i++) {
Thread.sleep(500);
System.out.println("---第" + i + "層---");
if (upFloorList.contains(i)) {
System.out.println(" ☆開門☆");
nowFloor = i;
upFloorList.remove(upFloorList.indexOf(i));
storeyWeight[i - bottomFloor] = 0;
if (upFloorList.size() > 0) {
System.out.println("剩余所選層數(shù)為:");
Iterator it = upFloorList.iterator();
while (it.hasNext()) {
int floor = (int) it.next();
System.out.print(floor + "層 重量:" + storeyWeight[floor - bottomFloor] + " ");
}
System.out.println();
}
return;
}
}
}
// 下行
if (downFloorList.size() > 0) {
System.out.println("---電梯下行---");
for (int i = nowFloor - 1; i >= bottomFloor; i--) {
Thread.sleep(500);
System.out.println("---第" + i + "層---");
if (downFloorList.contains(i)) {
System.out.println(" ☆開門☆");
nowFloor = i;
downFloorList.remove(downFloorList.indexOf(i));
storeyWeight[i - bottomFloor] = 0;
if (downFloorList.size() > 0) {
System.out.println("剩余所選層數(shù)為:");
Iterator it = downFloorList.iterator();
while (it.hasNext()) {
int floor = (int) it.next();
System.out.print(floor + "層 重量:" + storeyWeight[floor - bottomFloor] + " ");
}
System.out.println();
}
return;
}
}
}
System.out.println("無客");
}
}
2.2 程序入口
package com.company;
import Ele.Elevator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws InterruptedException {
// 創(chuàng)建一個電梯
int bottomFloor = 1; // 最低層1樓
int topFloor = 12; // 最高層12樓
int capacity = 1000; // 最大承重1000
Elevator elvator = new Elevator(bottomFloor, topFloor, capacity);
System.out.println("當(dāng)前電梯可選擇" + bottomFloor + "-" + topFloor + "層,請選擇樓層數(shù)(輸入-1表示關(guān)閉電梯門):");
//輸入內(nèi)容
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
//如果輸入不是數(shù)字,提示后,再次輸入
if (!scanner.hasNextInt()) {
System.out.println("請輸入數(shù)字!");
scanner.next();
}
//輸入是數(shù)字則進(jìn)行以下操作
else {
int num = scanner.nextInt();
//若輸入數(shù)字為-1,意為結(jié)束輸入,啟動電梯
if (num == -1) {
System.out.println("------------------------");
System.out.println("電梯門關(guān)閉,開始啟動");
elvator.StartElevator();
} else if (num > topFloor || num < bottomFloor || num == 0) {
//若輸入數(shù)字不符合樓層數(shù),則提示并再次輸入
System.out.println("請選擇1-12樓層。");
} else {
elvator.SetFloor(num);
}
}
}
}
}
三、總結(jié)
這個簡易電梯程序,基本實現(xiàn)了電梯的上行和下行判斷,當(dāng)選擇多個樓層時,可以對同為上行或下行的目標(biāo)樓層自動排序依次到達(dá),每個目標(biāo)樓層會隨機(jī)生成乘客重量并記錄。
在寫這個程序時,遇見了一些問題:
1. 使用while語句接收用戶輸入時,判斷輸入是否為數(shù)字,輸入不是數(shù)字會陷入死循環(huán)提示。在此增加了scanner.next()語句,提示后可以繼續(xù)輸入。
if (!scanner.hasNextInt()) {
System.out.println("請輸入數(shù)字!");
scanner.next();
}
2. 若重復(fù)選擇某樓層,到達(dá)該樓層后,仍會顯示該樓層為剩余樓層。在此增加了判斷語句,如果選擇的樓層數(shù)已經(jīng)存在于上升或下降目標(biāo)樓層的集合中,則只增加重量,不會重復(fù)添加目標(biāo)樓層。
if (!upFloorList.contains(floorNum) && !downFloorList.contains(floorNum)) {
if (floorNum > nowFloor) {
upFloorList.add(floorNum);
// 上升樓層升序排序
Collections.sort(upFloorList);
} else {
downFloorList.add(floorNum);
// 下降樓層降序排序
downFloorList.sort(Collections.reverseOrder());
}
}
}
3. 將目標(biāo)樓層隨機(jī)產(chǎn)生的重量存放于一個數(shù)組中,當(dāng)前樓層減最低層,就是當(dāng)前層重量的下標(biāo),假如當(dāng)前樓層為5樓,5樓下標(biāo)就是 5-1 = 4,storeyWeight[4]即為5樓重量。
代碼還有不完善的地方,例如若從1到8層上升時,6層有人也要乘坐電梯,如何滿足在6層停止并上人后,繼續(xù)上行。這些還有待我繼續(xù)完善,也望請大家批評指正
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Springboot+gateway整合依賴并處理依賴沖突問題
這篇文章主要介紹了Springboot+gateway整合依賴并處理依賴沖突問題,給大家提到了spring boot版本和spring cloud版本,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
Spring實現(xiàn)內(nèi)置監(jiān)聽器
這篇文章主要介紹了Spring 實現(xiàn)自定義監(jiān)聽器案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧,希望能給你帶來幫助2021-07-07
Java獲取當(dāng)前時間并轉(zhuǎn)化為yyyy-MM-dd?HH:mm:ss格式的多種方式
這篇文章主要介紹了Java獲取當(dāng)前時間并轉(zhuǎn)化為yyyy-MM-dd?HH:mm:ss格式的多種方式,每種方式結(jié)合實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03
springcloud微服務(wù)基于redis集群的單點(diǎn)登錄實現(xiàn)解析
這篇文章主要介紹了springcloud微服務(wù)基于redis集群的單點(diǎn)登錄實現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09

