import java.awt.AWTException;
import java.awt.Robot;
import java.util.Scanner;
public class Elevator {
public int layers;
//This is a program just like the running process of elevator.
public static void main(String[] args) throws AWTException{
Elevator elevator = new Elevator();
elevator.layers=1;
while(true){
System.out.println("");
System.out.println("全楼楼高25层,欢迎乘坐电梯。");
System.out.println("目前电梯正在"+elevator.layers+"楼等待。");
int start = elevator.beforeRide(elevator.layers);
int end = elevator.afterRide(start);
elevator.layers = end;
}
}
public int beforeRide(int layers) throws AWTException{
Scanner in=new Scanner(System.in);
System.out.println("请问您在几楼?");
int start = in.nextInt();
Robot timer = new Robot();
timer.delay(1000);
if(start>layers){
for(int i=start;i<layers;i++){
System.out.println("电梯上行,到达"+i+"层。");
timer.delay(1000);
}
System.out.println("电梯上行,到达"+start+"层,电梯开门。");
}
if(start<layers){
for(int i=start;i>layers;i--){
System.out.println("电梯下行,到达"+i+"层。");
timer.delay(1000);
}
System.out.println("电梯下行,到达"+start+"层,电梯开门。");
}
if(start==layers){
System.out.println("电梯开门。");
}
return start;
}
public int afterRide(int start) throws AWTException{
System.out.println("您已进入电梯。");
Robot timer = new Robot();
Scanner in=new Scanner(System.in);
in=new Scanner(System.in);
System.out.println("请问您准备到几楼?");
int end = in.nextInt();
timer.delay(1000);
System.out.println("您从"+start+"楼出发,正在前往"+end+"楼。");
if(start<end){
for(int i=start;i<end;i++){
System.out.println("电梯上行,到达"+i+"层。");
timer.delay(1000);
}
System.out.println("电梯上行,到达"+end+"层,电梯开门。");
timer.delay(1000);
}
if(start>end){
for(int i=start;i>end;i--){
System.out.println("电梯下行,到达"+i+"层。");
timer.delay(1000);
}
System.out.println("电梯下行,到达"+end+"层,电梯开门。");
timer.delay(1000);
}
if(start==end){
System.out.println("电梯开门。");
timer.delay(1000);
}
return end;
}
}
随手练之模拟电梯运行
最新推荐文章于 2022-04-10 13:29:26 发布