在电梯系统中增加两个回路?

时间:2014-03-25 12:50:18

标签: java loops

所以我正在创建一个电梯系统来配合另一段代码。系统目前工作正常,但是我想添加一个while循环,这样当选择了无效的楼层时,我有机会在代码中重新尝试另一层;

public static void selectFloor() {
        Scanner scnr = new Scanner(System.in);
        int newFloor;

        System.out.println("Enter your destination floor >>> ");
        newFloor = scnr.nextInt();
        if (newFloor > 7 || newFloor < 0) {
            System.out.println("Invalid floor entry");
        }

我还想为系统添加另一个循环,以便一旦选择了一个楼层并且电梯到达目的地楼层,该循环将允许用户在永无止境的循环中从当前楼层选择另一个目的地。这是代码的其余部分;

        else {  
            int direction = 0;
            if(currentFloor < newFloor){
                direction = 1; 
            } else if (currentFloor > newFloor) {
                direction = -1; ;
            } else {
                direction = 0; 
            }
            for (; currentFloor != newFloor; currentFloor += direction)
                System.out.println("..." + currentFloor);
                System.out.println("Elevator has arrived!");
        }
    }

    public void fireAlarm() {
        System.out.println("***FIRE ALARM*** Please exit the building safely.");


}

}

由于我的代码结构,我无法弄清楚如何做到这一点。我怎么能添加这两个循环?

3 个答案:

答案 0 :(得分:0)

你显然需要一个循环,它将永远循环,直到发送键盘输入的中断信号(假设这是数字-1

public static void selectFloor() {
    Scanner scnr = new Scanner(System.in);
    int newFloor= 0; //initalize to 0

    while (newFloor != -1) { //loop forever until user gives -1 as input
        System.out.println("Enter your destination floor >>> ");
        System.out.println("To exit the programm enter: -1"); //tell the user how to exit
        newFloor = scnr.nextInt();
        if (newFloor > 7 || newFloor < 0) {
           //if logic
        } else {  
           //else logic
        }
    }
}

答案 1 :(得分:0)

你可以这样试试:

Scanner scnr = new Scanner(System.in);
int newFloor;
while (true) {
    System.out.print("Enter your destination floor >>> ");
    newFloor = scnr.nextInt();
    if (newFloor >= 0 && newFloor <= 7) break;
    System.out.println("Invalid floor entry");
};

int direction = Math.signum(newFloor - currentFloor);
while (currentFloor != newFloor) {
    currentFloor += direction;
    System.out.println("..." + currentFloor);
}
System.out.println("Elevator has arrived!");    }

答案 2 :(得分:0)

这是我的方法。我将整个互动分解为三种方法:第一种&#34;讲述故事&#34;。第二个处理新楼层的输入,第三个处理电梯到达目的地楼层。因此,每种方法都有明确的责任,如果需要,可以在以后阶段进行修改。

// Handles the whole thing
public static void elevatorAction() {
    int currentFloor = 1;
    int destinationFloor;

    while(true) {
        destinationFloor = selectNewFloor();
        goToDestinationFloor(currentFloor, destinationFloor);
        System.out.println("The elevator has arrived");
        currentFloor = destinationFloor;
    }
}

// Handles the traveling
public static void goToDestinationFloor(int currentFloor, int destinationFloor) {
    int increment;

    if(currentFloor == destinationFloor) {
        return;
    }

    increment = (currentFloor < destinationFloor? 1: -1);
    while(currentFloor != destinationFloor) {
        currentFloor += increment;
        System.out.println("..." + currentFloor);
    }
}

// Lets the user select a new destination floor for the elevator
public static int selectNewFloor() {
    Scanner scnr = new Scanner(System.in);
    int tempNewFloor;

    System.out.println("Please choose your destination floor.");
    while((true) {
        tempNewFloor = scnr.nextInt();
        if(tempNewFloor < 0 || tempNewFloor > 7) {
            System.out.println("Next floor: " + tempNewFloor);
            break;
         } else {
            System.out.println("Invalid input: Please choose a floor between 1 and 7");
        }
    }
}