欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Java的電梯系統(tǒng)實(shí)現(xiàn)過(guò)程

 更新時(shí)間:2019年10月24日 10:27:31   作者:百畝  
這篇文章主要介紹了基于Java的電梯系統(tǒng)實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、思路

寫一個(gè)簡(jiǎn)單的電梯系統(tǒng),首先根據(jù)老師提供的需求,寫一下基礎(chǔ)思路:

  • 電梯有最高層和最低層,輸入數(shù)字選擇正確樓層數(shù)
  • 輸入數(shù)字大于當(dāng)前樓層,則為上行;小于當(dāng)前樓層,則為下行
  • 每次輸入數(shù)字的時(shí)候,需要對(duì)同為上行的數(shù)字或者同為下行的數(shù)字,進(jìn)行排序
  • 所輸入的目標(biāo)樓層用集合存放,循環(huán)最低層到最高層,如果當(dāng)前層在集合中存在,顯示開(kāi)門,若還有目標(biāo)樓層,則關(guān)門,繼續(xù)到下一目標(biāo)樓層。
  • 當(dāng)選擇一個(gè)目標(biāo)樓層,會(huì)生成隨機(jī)重量記錄在目標(biāo)樓層,上行用原來(lái)重量加上目標(biāo)樓層重量,下行則用原來(lái)重量減去目標(biāo)樓層重量

二、實(shí)現(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("請(qǐng)選擇其它樓層");
      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 + "層上來(lái)重量:" + thisFloorWeight + ",此時(shí)總重:" + (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());
      }
    }
  }

  // 上升:從所在層到所選樓層中的最高層
  // 下降:從所在層到所選樓層中的最低層
  // 獲得集合中最后一個(gè)元素:list.get(list.size()-1);

  // 啟動(dòng)電梯
  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(" ☆開(kāi)門☆");
          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(" ☆開(kāi)門☆");
          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("無(wú)客");

  }
}

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)建一個(gè)電梯
    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 + "層,請(qǐng)選擇樓層數(shù)(輸入-1表示關(guān)閉電梯門):");

    //輸入內(nèi)容
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNextLine()) {
      //如果輸入不是數(shù)字,提示后,再次輸入
      if (!scanner.hasNextInt()) {
        System.out.println("請(qǐng)輸入數(shù)字!");
        scanner.next();
      }

      //輸入是數(shù)字則進(jìn)行以下操作
      else {
        int num = scanner.nextInt();
        //若輸入數(shù)字為-1,意為結(jié)束輸入,啟動(dòng)電梯
        if (num == -1) {
          System.out.println("------------------------");
          System.out.println("電梯門關(guān)閉,開(kāi)始啟動(dòng)");
          elvator.StartElevator();
        } else if (num > topFloor || num < bottomFloor || num == 0) {
          //若輸入數(shù)字不符合樓層數(shù),則提示并再次輸入
          System.out.println("請(qǐng)選擇1-12樓層。");
        } else {
          elvator.SetFloor(num);
        }
      }
    }
  }
}

三、總結(jié)

這個(gè)簡(jiǎn)易電梯程序,基本實(shí)現(xiàn)了電梯的上行和下行判斷,當(dāng)選擇多個(gè)樓層時(shí),可以對(duì)同為上行或下行的目標(biāo)樓層自動(dòng)排序依次到達(dá),每個(gè)目標(biāo)樓層會(huì)隨機(jī)生成乘客重量并記錄。

在寫這個(gè)程序時(shí),遇見(jiàn)了一些問(wèn)題:

1. 使用while語(yǔ)句接收用戶輸入時(shí),判斷輸入是否為數(shù)字,輸入不是數(shù)字會(huì)陷入死循環(huán)提示。在此增加了scanner.next()語(yǔ)句,提示后可以繼續(xù)輸入。

 if (!scanner.hasNextInt()) {
        System.out.println("請(qǐng)輸入數(shù)字!");
        scanner.next();
      }

2. 若重復(fù)選擇某樓層,到達(dá)該樓層后,仍會(huì)顯示該樓層為剩余樓層。在此增加了判斷語(yǔ)句,如果選擇的樓層數(shù)已經(jīng)存在于上升或下降目標(biāo)樓層的集合中,則只增加重量,不會(huì)重復(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)生的重量存放于一個(gè)數(shù)組中,當(dāng)前樓層減最低層,就是當(dāng)前層重量的下標(biāo),假如當(dāng)前樓層為5樓,5樓下標(biāo)就是 5-1 = 4,storeyWeight[4]即為5樓重量。

代碼還有不完善的地方,例如若從1到8層上升時(shí),6層有人也要乘坐電梯,如何滿足在6層停止并上人后,繼續(xù)上行。這些還有待我繼續(xù)完善,也望請(qǐng)大家批評(píng)指正

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java遞歸求和1+2+3+...+n實(shí)例詳解

    Java遞歸求和1+2+3+...+n實(shí)例詳解

    在本篇文章里小編給大家?guī)?lái)了關(guān)于Java遞歸求和1+2+3+...+n實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。
    2020-01-01
  • 關(guān)于Springboot+gateway整合依賴并處理依賴沖突問(wèn)題

    關(guān)于Springboot+gateway整合依賴并處理依賴沖突問(wèn)題

    這篇文章主要介紹了Springboot+gateway整合依賴并處理依賴沖突問(wèn)題,給大家提到了spring boot版本和spring cloud版本,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • 淺談Maven Wrapper

    淺談Maven Wrapper

    這篇文章主要介紹了淺談Maven Wrapper,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 原生Java操作兔子隊(duì)列RabbitMQ

    原生Java操作兔子隊(duì)列RabbitMQ

    這篇文章主要介紹了原生Java操作兔子隊(duì)列RabbitMQ,MQ全稱為Message?Queue,即消息隊(duì)列,“消息隊(duì)列”是在消息的傳輸過(guò)程中保存消息的容器,需要的朋友可以參考下
    2023-05-05
  • Spring實(shí)現(xiàn)內(nèi)置監(jiān)聽(tīng)器

    Spring實(shí)現(xiàn)內(nèi)置監(jiān)聽(tīng)器

    這篇文章主要介紹了Spring 實(shí)現(xiàn)自定義監(jiān)聽(tīng)器案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧,希望能給你帶來(lái)幫助
    2021-07-07
  • IDEA全局查找關(guān)鍵字的用法解讀

    IDEA全局查找關(guān)鍵字的用法解讀

    這篇文章主要介紹了IDEA全局查找關(guān)鍵字的用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java使用Socket通信傳輸文件的方法示例

    Java使用Socket通信傳輸文件的方法示例

    這篇文章主要介紹了Java使用Socket通信傳輸文件的方法,結(jié)合實(shí)例形式分析了java socket編程實(shí)現(xiàn)文件傳輸操作的相關(guān)技巧,需要的朋友可以參考下
    2017-06-06
  • 利用SpringBoot解決多個(gè)定時(shí)任務(wù)阻塞的問(wèn)題

    利用SpringBoot解決多個(gè)定時(shí)任務(wù)阻塞的問(wèn)題

    當(dāng)我們?cè)赟pring Boot應(yīng)用中使用多個(gè)定時(shí)任務(wù)時(shí),任務(wù)之間的阻塞可能是一個(gè)常見(jiàn)的問(wèn)題,這可能會(huì)因任務(wù)之間的依賴、執(zhí)行時(shí)間過(guò)長(zhǎng)或資源爭(zhēng)用等原因而發(fā)生,本文讓我們深入探討如何利用Spring Boot來(lái)解決多個(gè)定時(shí)任務(wù)阻塞的問(wèn)題,感興趣的小伙伴跟著小編一起來(lái)看看吧
    2024-01-01
  • Java獲取當(dāng)前時(shí)間并轉(zhuǎn)化為yyyy-MM-dd?HH:mm:ss格式的多種方式

    Java獲取當(dāng)前時(shí)間并轉(zhuǎn)化為yyyy-MM-dd?HH:mm:ss格式的多種方式

    這篇文章主要介紹了Java獲取當(dāng)前時(shí)間并轉(zhuǎn)化為yyyy-MM-dd?HH:mm:ss格式的多種方式,每種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • springcloud微服務(wù)基于redis集群的單點(diǎn)登錄實(shí)現(xiàn)解析

    springcloud微服務(wù)基于redis集群的單點(diǎn)登錄實(shí)現(xiàn)解析

    這篇文章主要介紹了springcloud微服務(wù)基于redis集群的單點(diǎn)登錄實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評(píng)論