Java 中的电梯模拟器帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15602830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:13:39  来源:igfitidea点击:

Elevator Simulator help in Java

javasimulation

提问by user2120893

I'm having trouble finishing a basic elevator simulator in Java. What I have so far is an option that lets the user input whether they want to choose a floor, to pull a fire alarm, or to quit the simulation. When they choose select floor, they can pick any floor from 1 to 100, except 13. What I can't figure out how to do is to get the simulation to track their current floor so that they can go down. This is what I have so far:

我在用 Java 完成基本的电梯模拟器时遇到了麻烦。到目前为止,我有一个选项,让用户输入他们是要选择楼层、拉火警还是退出模拟。当他们选择 select floor 时,他们可以选择从 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!");
    }
}

Also, would it be helpful to post my other class for this program?

另外,为这个程序发布我的其他课程会有帮助吗?

回答by raptortech97

The Elevatorshould have a currentFloorfield, like so:

Elevator应该有一个currentFloor领域,像这样:

private int currentFloor;

Then, in selectFloor, you need to find the direction. Also, in selectFloor, the else ifis unnecessary.

然后,在 中selectFloor,您需要找到方向。此外,在 中selectFloorelse 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!");
    }
}

Note: I haven't tested this yet, so I can't be sure it's correct.

注意:我还没有测试过这个,所以我不能确定它是正确的。

回答by Kyle

Give your Elevatorobject a class variable by adding a private int floor;directly under the class opening tag. (Above the Elevator class constructor.) This variable will be tied directly to the Elevator object that contains it.

Elevator通过private int floor;在类开始标记下直接添加一个,为您的对象提供一个类变量。(在 Elevator 类构造函数之上。)这个变量将直接绑定到包含它的 Elevator 对象。

That way when you create your Elevator by using new Elevatoryou'll also have an int value always available to hold the floor. To access this value, build a getter and setter method. They should look like the following:

这样,当您通过使用创建 Elevator 时,new Elevator您还将拥有一个始终可用的 int 值来固定地板。要访问此值,请构建一个 getter 和 setter 方法。它们应该如下所示:

public void setFloor(int floor) {
    this.floor = floor;
}

public int getFloor() {
    return floor;
}

You can then call these two methods to set the floor number and get the floor number. To keep track, in your selectFloormethod you'll need to use setFloor and pass it the floor number after a valid selection is made. You could then use getFloor to determine whether it would be going up or down.

然后就可以调用这两个方法来设置楼层号并获取楼层号。为了跟踪,在您的selectFloor方法中,您需要使用 setFloor 并在做出有效选择后将楼层编号传递给它。然后您可以使用 getFloor 来确定它是上升还是下降。

To have your constructor set the floorvariable at 1 when a new Elevator object is created. Simply change your constructor to look like this:

floor在创建新 Elevator 对象时让您的构造函数将变量设置为 1。只需将构造函数更改为如下所示:

public Elevator() {
    setFloor(1);
}

Hope this helps! If you have any questions on how those things are working let me know, I'll try to provide more details.

希望这可以帮助!如果您对这些事情的工作方式有任何疑问,请告诉我,我会尽力提供更多详细信息。

回答by Chris

I just inserted a new method called backToBasement() and tied it into your selectFloor() method. Hope its helpful.

我刚刚插入了一个名为 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");

    }