Simple Elevator Program

in #steemiteducation7 years ago

As of recent, I kinda got bored and decided to make a simple elevator program using recursion. What is recursion? Recursion is calling a method inside of itself. It's like a cycle. The water cycle, per se. Rain falls, evaporates, falls again.

    // This is the method that we will be using. 
    public static void NextFloor(int num) {
 
        // Always ask the user where they would like to go.
        System.out.println("Now where would you like to go?");

        // Get the next location
        int nextFloor = scanner.nextInt();

        // Inform the user that we are moving
        System.out.println("Going to floor " + nextFloor + " from floor " + num);

        // We can now call the method and start recursioning! (is that even a word?)
        NextFloor(nextFloor);
    }

As you can see, we are calling NextFloor inside of itself. But what I triggering the method to start? In our main method of course.

// Import the scanner so that we can take in user input
import java.util.Scanner;

//Our basic starter
public class Elevator {
  
    // Make our scanner public so that we can call it in more than one method!
    public static Scanner scanner = new Scanner(System.in); 

    // Our Main Method - where we will be calling the function
    public static void main(String[] args) {

        // Ask the user where they would like to go.
        System.out.println("Which floor would you like to go to?");

        // Get the floor they want to go to.
        int floor = scanner.nextInt();
                
        // Its safe to assume the user is starting on the first floor.
        int currentFloor = 0;
            
        // Let the user know we are moving!
        System.out.println("Now moving to floor " + floor + " from floor " + currentFloor);

        // We could ignore this part and just say "NextFloor(floor) but your choice.
                currentFloor = floor;

        // Now we can call the method 
        NextFloor(currentFloor);
    }

Now that we have everything we needed defined, lets put it all together!

import java.util.Scanner;
public class Elevator {
    public static Scanner scanner = new Scanner(System.in); 
    public static void main(String[] args) {
        System.out.println("Which floor would you like to go to?");
        int floor = scanner.nextInt();
        Boolean isMoving = true;
        int currentFloor = 0;
        System.out.println("Now moving to floor " + floor + " from floor " + currentFloor);
        currentFloor = floor;
        NextFloor(currentFloor);
    }
    public static void NextFloor(int num) {
        System.out.println("Now where would you like to go?");
        int nextFloor = scanner.nextInt();
        System.out.println("Going to floor " + nextFloor + " from floor " + num);
        NextFloor(nextFloor);
    }
}

Now, I challenge thee to make it better, detect when its moving, where its at while its moving, and maybe add a cooldown!

Sort:  

Congratulations @zothuro! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @zothuro! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.22
TRX 0.25
JST 0.039
BTC 95470.30
ETH 3313.37
USDT 1.00
SBD 3.15