我需要拒绝访问某个函数,直到达到特定值为止。例如,让函数休眠一个变量的值5-10,然后在11上执行该操作,该变量由用户手动加1。我该如何设置?
这是运行Java的基于回合的资源管理游戏。我尝试将方法设置为仅在布尔值为true时运行,而在不应该运行时为false。
控制开关语句
case "farm":
if (farmAvailable = true) {
farmAvailable = false;
a.foodManagement(foodGenerator, food, storageBunker, day,
farmAvailable);
} else if (farmAvailable = false) {
System.out.println("Farming in progress...");
}
break;
功能受到控制
public int foodManagement(int generator, int currentStorage, int
storageBunker, int day, boolean available) {
int food = generator * 12;
if (available == true) {
int rest = day + 5;
available = false;
while (rest < day) {
if (currentStorage + food > (storageBunker * 25)) {
currentStorage += food;
food = currentStorage - (storageBunker * 25);
System.out.println("Food storage full.");
available = true;
return food;
} else {
available = true;
return food;
}
}
} else {
return 0;
}
return 0;
}
当farmAvailable为false时,该方法不应运行,但是当您提示程序这样做时,什么也不会发生。