我在使用Java完成基本的电梯模拟器时遇到了麻烦。到目前为止,我所提供的选项是让用户输入他们是想选择楼层,拉火警还是退出模拟。当他们选择选择楼层时,他们可以从1到100之间选择任何楼层,除了13.我无法弄清楚如何做的是让模拟跟踪他们当前的楼层以便他们可以下降。这就是我到目前为止所做的:
public class Elevator {
public Elevator() {}
public void selectFloor() {
Scanner scnr = new Scanner(System.in);
int newFloor;
System.out.println("Enter the floor you'd like to go to ==> ");
newFloor = scnr.nextInt();
if (newFloor > 100 || newFloor < 0 || newFloor == 13) {
System.out.println("Invalid selection");
}
else if (newFloor <= 100 && newFloor > 0 && newFloor != 13) {
for (int i = 1; i <= newFloor; i++)
System.out.println("..." + i);
System.out.println("Ding!");
}
}
public void fireAlarm() {
System.out.println("Danger, you must exit the building now!");
}
}
另外,为这个程序发布我的其他课程会有帮助吗?
答案 0 :(得分:2)
Elevator
应该有一个currentFloor
字段,如下所示:
private int currentFloor;
然后,在selectFloor
中,您需要找到方向。此外,在selectFloor
中,else if
是不必要的。
public class Elevator {
private int currentFloor;
public Elevator() {
currentFloor = 0;
}
public void selectFloor() {
Scanner scnr = new Scanner(System.in);
int newFloor;
System.out.println("Enter the floor you'd like to go to ==> ");
newFloor = scnr.nextInt();
if (newFloor > 100 || newFloor < 0 || newFloor == 13) {
System.out.println("Invalid selection");
}
else { // The if was not necessary
int direction = 0;
if(currentFloor < newFloor){
direction = 1; // going up;
} else if (currentFloor > newFloor) {
direction = -1; //going down;
} else {
direction = 0; //going nowhere;
}
for (; currentFloor != newFloor; currentFloor += newFloor)
System.out.println("..." + i);
System.out.println("Ding!");
}
}
public void fireAlarm() {
System.out.println("Danger, you must exit the building now!");
}
}
注意:我还没有测试过,所以我不能确定它是否正确。
答案 1 :(得分:0)
通过在类开头标记下直接添加Elevator
,为您的private int floor;
对象提供类变量。 (在Elevator类构造函数之上。)此变量将直接绑定到包含它的Elevator对象。
这样,当您使用new Elevator
创建Elevator时,您还可以使用一个int值来保持最低限度。要访问此值,请构建getter和setter方法。它们应如下所示:
public void setFloor(int floor) {
this.floor = floor;
}
public int getFloor() {
return floor;
}
然后,您可以调用这两种方法来设置楼层编号并获取楼层编号。要跟踪,在selectFloor
方法中,您需要使用setFloor并在进行有效选择后将其传递给楼层编号。然后,您可以使用getFloor来确定它是上升还是下降。
要在创建新Elevator对象时让构造函数将floor
变量设置为1。只需将构造函数更改为如下所示:
public Elevator() {
setFloor(1);
}
希望这有帮助!如果您对这些工作如何工作有任何疑问,请告诉我,我会尝试提供更多详细信息。
答案 2 :(得分:0)
我刚刚插入了一个名为backToBasement()的新方法,并将其绑定到selectFloor()方法中。希望它有所帮助。
public void selectFloor() {
Scanner scnr = new Scanner(System.in);
int newFloor;
System.out.println("Enter the floor you'd like to go to ==> ");
newFloor = scnr.nextInt();
if (newFloor > 100 || newFloor < 0 || newFloor == 13) {
System.out.println("Invalid selection");
}
else if (newFloor <= 100 && newFloor > 0 && newFloor != 13) {
for (int i = 1; i <= newFloor; i++)
System.out.println("..." + i);
System.out.println("Ding!");
backToBasement(newFloor);
}
}
public void fireAlarm() {
System.out.println("Danger, you must exit the building now!");
}
public void backToBasement(int newFloor){
for (int i=newFloor; i>0;i--){
System.out.println("..." + i);
}
System.out.println("Ding!... Back to Ground Level");
}