可能有点小bug。
import java.util.*;
public class Main {
final static int CAPACITY=1000; //电梯最大重量1000kg(通过手动输入或自动产生重量)
final static int TOP_FLOOR = 12; //电梯最高层
final static int BOTTOM_FLOOR = 1; //电梯最底层
static int lift_wigth = 0;//电梯载重
static int lift_pointer = 1;//电梯所在层数,
static int lift_direction = 1;//>0向上走 ,<0向下走
static Scanner cin = new Scanner(System.in);
public static void main(String[] args) throws InterruptedException {
Set<Integer> goals = new TreeSet<Integer>();
System.out.println("该楼层总楼层为12层,第一层编号为一层");
while(true){
goals = into_lift(goals);//进入电梯
goals = startup_lift(goals);//电梯启动
}
}
public static Set<Integer> into_lift(Set<Integer> goals){//进入电梯
System.out.println("当前楼层在" + lift_pointer+"。请输入目标楼层,以-1结束:");
Boolean flag = false;
while(true){
String goal = cin.next();
if(goal.equals("-1")){
break;
}else if(goal.length()==1&&(goal.charAt(0)>'0'&&goal.charAt(0)<='9')){
goals.add(Integer.parseInt(goal));
flag = true;
}else if(goal.equals("10")||goal.equals("11")||goal.equals("12")) {
goals.add(Integer.parseInt(goal));
flag = true;
}
else {
System.out.println("“"+goal+"”未被输入。请输入正确楼层编号!");
}
}
if(flag == false) {
System.out.println("没有乘客上电梯");
return goals;
}
int temp_wight = (int)(Math.random()*100 + 200);//随机生成200~299的数,为进入电梯的重量
System.out.println("进入电梯的重量为:"+temp_wight+"kg");
lift_wigth += temp_wight;
if(lift_wigth > CAPACITY){//超重
System.out.println("天梯超重,无法关门!");
System.out.println("质量高的人默默地离开了电梯。。。");
lift_wigth = (int)(Math.random()*100 + 900);
}
System.out.println("电梯载重:" + lift_wigth+"kg,电梯出发!");
return goals;
}
public static Set<Integer> startup_lift(Set<Integer> goals) throws InterruptedException{//电梯启动
if(goals.size()==0) {
System.out.println("电梯无人使用,回到1层。");
goals.add(1);
}
if(goals.size()==1) {//一个目标楼层时控制电梯向上还是向下
int temp_goal = goals.iterator().next();
if(lift_pointer < temp_goal) {
lift_direction = 1;
}
else {
lift_direction = -1;
}
}else {//多个目标楼层时控制电梯向上还是向下
if(lift_direction == -1) {//电梯方向朝下,如果最低的目标楼层大于当前楼层,调整向上
if(goals.iterator().next() < lift_pointer) {
lift_direction = 1;
}
} else {//电梯方向朝上,如果所有目标楼层小于当前楼层,调整向下
int flag = 0;
for(int goal:goals) {
if(goal > lift_pointer) {
flag = 1;
break;
}
}
if(flag == 0) {
lift_direction = -1;
}
}
}
if(lift_direction > 0){//电梯向上时,找到第一个大于当前楼层的目标停下
Iterator<Integer> iter = goals.iterator();
while(iter.hasNext()){
int goal = iter.next();
if(goal > lift_pointer){
for(int i = lift_pointer ;i <= goal ; i++){
System.out.println("电梯在第"+i+"层楼---");
Thread.sleep(500);
}
down_lift(goals.size());
lift_pointer = goal;
iter.remove();
break;
}
}
}
else {//电梯向下时,找到第一个小于当前楼层的目标停下
Iterator<Integer> iter = goals.iterator();
int temp_goal = 12;
while(iter.hasNext()){
int goal = iter.next();
if(goal < lift_pointer){
temp_goal = goal;
}
else {
break;
}
}
for(int i = lift_pointer ; i>= temp_goal ; i--){
System.out.println("电梯在第"+i+"层楼---");
Thread.sleep(500);
}
down_lift(goals.size());
lift_pointer = temp_goal;
Iterator<Integer> iter2 = goals.iterator();
while(iter2.hasNext()){
if(iter2.next() == temp_goal){
iter2.remove();
}
}
}
return goals;
}
public static void down_lift(int goals_length) throws InterruptedException{//乘客离开处理电梯重量
int temp_wight;
if(goals_length == 1){//最后的楼层所有人都下电梯
temp_wight = lift_wigth;
}
else {
temp_wight = (int)(Math.random()*100 + 100);
}
lift_wigth -= temp_wight;
System.out.println("有乘客下电梯,离开电梯的重量为:" + temp_wight);
Thread.sleep(1000);
System.out.println("电梯当前载重:" + lift_wigth + "kg。");
}
}