有人知道如何获得一个函数的值吗?该函数指示街区上是否有空停车位。我正在使用一个Queue,我尝试了函数nFree()
和spaceIndex()
,但我发现这两个函数并不是为类型定义的
希望我理解你的问题,下面是可以返回所有停车空位的代码。
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;
}
}