电梯捎带系统

核心功能类

package com.daiju;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
public class Main {
	public static final int MAX_WEIGHT = 1000;
	public static final int MAX_FLOOR = 12;
	public static final int MIN_FLOOR = 1;
	public int currentFloor = 1;
	public boolean up = true;
	//随机生成体重
	public int randomWeight(){
		Random random = new Random();
		return random.nextInt(1441)+60;	
	}	
	//判断是否超重
	public boolean checkWeight(int currentWeiht){		
		return MAX_WEIGHT>=currentWeiht;
	}	
	//记录所按的楼层
	public Set<Integer> floors(){
		Scanner input = new Scanner(System.in);
		System.out.println("请输入楼层,多楼层之间用空格隔开,回车键结束输入!!!");
		String str = input.nextLine();
		String[] nums = str.split(" ");
		Set<Integer> finalNums = new HashSet();
		for(int i=0;i<=nums.length-1;i++){
			//排除输入当前所在楼层
			if(!nums[i].equals(currentFloor)){
				finalNums.add(Integer.parseInt(nums[i]));
			}			
		}
		if(Collections.max(finalNums)>MAX_FLOOR || Collections.min(finalNums)<MIN_FLOOR){
			System.out.println("输入的电梯楼层有误,请重新输入");
			return floors();
		}
		return finalNums;
	}
	//运行前的重量判断
	public void beforeRun(){
		if(checkWeight(randomWeight())){
			System.out.println("关门,当前楼层:"+currentFloor);

		}else{
			System.out.println("超重!!!关门,当前楼层:"+currentFloor);
		}
	}
	//楼层判断抽取
	public void goFloor(Set<Integer> floors,int currentFloor) throws Exception{
		System.out.println("当前楼层:"+currentFloor);
		Thread.sleep(1500);				
		if(floors.contains(currentFloor)){
			System.out.println("到达楼层:"+currentFloor+" 开门");
			floors.remove(currentFloor);
			beforeRun();
			Thread.sleep(1500);
		}
	}
	//运行功能
	public void runFunction(Set<Integer> floors,int flag) throws Exception{
		if(up&&floors.size()>0){
			if(flag==0){
				beforeRun();
			}
			while(floors.size()>0&&Collections.max(floors)>currentFloor){				
				currentFloor++;
				goFloor(floors,currentFloor);
			}
			up = false;
			runFunction(floors,1);
		}
		if(!up&&floors.size()>0){
			if(flag==0){
				beforeRun();
			}
			while(floors.size()>0&&Collections.max(floors)<currentFloor){
				currentFloor--;
				goFloor(floors,currentFloor);
			}
			up = true;
			runFunction(floors,1);
		}	
	}	
}

测试类

package com.daiju;

public class Test {
	public static void main(String[] args){
		Main main = new Main();
		try {
			while(true){
			main.runFunction(main.floors(),0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值