-1

有没有人知道如何获得一个函数的值,该函数指示街区是否有空停车位。我正在使用队列,我尝试了函数nFree()and spaceIndex(),但我知道这两个函数没有为类型定义

4

1 回答 1

-1

希望我理解您的问题,下面是可能返回所有停车位的代码。

public class Parking {

    static int parkingSize = 10;

    static Object[] object = new Object[parkingSize];

    public static void main(String[] args) {

        add("java", 10);

        System.out.println(checkEmptyParkingSpot());

        emptyParkingSpot(10);

        System.out.println(checkEmptyParkingSpot());

    }

    // add the object in specicif location
    public static void add(String info, int position) {

        // if the position does not exist an erro will be thow
        if (position > object.length ) {
            throw new RuntimeException("The possition indicated does not exist in the park");
        }

        if (object[position - 1] != null) {
            throw new RuntimeException("The possition is ocupied");
        }

        object[position - 1] = info;

    }

    // to remove an object from the park and free the space
    public static void emptyParkingSpot(int position) {

        if (position > object.length ) {
            throw new RuntimeException("The possition indicated does not exist in the park");
        }

        if (object[position - 1] == null) {
            throw new RuntimeException("The possition isalready empty");
        }
        object[position - 1] = null;
    }

    // lists the empty spot 
    public static String checkEmptyParkingSpot() {

        String emptySpace = "".trim();

        for (int i = 0; i < object.length; i++) {

            // list the empty space
            if (object[i] == null) {
                emptySpace = emptySpace + (i + 1) + "\n";
            }

        }

        // if there is no space the message will be park full
        if (emptySpace.isEmpty()) {
            emptySpace = "Park is Full";
        }

        return emptySpace;
    }

}
于 2019-11-19T10:07:29.140 回答