我一直在练习Java编程,最近我想我会尝试使用不同的库来编写一些基于状态的脚本。我遇到了一个切换声明,我似乎无法进入第二种情况。现在我明白你们可能不熟悉外部API,但我只是想知道我的逻辑是否有问题。状态来自枚举,我有一个getState()方法来测试不同的先决条件:
private enum State {
CUT, WALK_TO_BANK, BANK, WALK_TO_TREE
}
private State getState() {
if (client.getInventory().isFull() && !BANK_AREA.contains(myPlayer()))
return State.WALK_TO_BANK;
if (!client.getInventory().isFull() && !CHOP_AREA.contains(myPlayer()))
return State.WALK_TO_TREE;
if (!client.getInventory().isEmpty() && BANK_AREA.contains(myPlayer()))
return State.BANK;
return State.CUT;
}
切换声明:
switch (getState()) {
case CUT:
if (!myPlayer().isAnimating() && equipmentTab.isWieldingWeapon(Axe)) {
log("You have an Axe.");
} else {
log("You don't have an axe");
State.WALK_TO_BANK.equals(true);
RS2Object bank = closestObjectForName("Bank Booth");
if (bank != null) {
if (bank.interact("Bank")) {
while (!client.getBank().isOpen())
sleep(250);
client.getBank().withdraw1(Axe);
equipmentTab.equipForNameThatContains(EquipmentSlot.WEAPON, "axe");
State.CUT.equals(true);
}
RS2Object tree = closestObject(TREE_ID);
if (!myPlayer().isInArea(CHOP_AREA) && client.getInventory().isEmpty()) {
State.WALK_TO_TREE.equals(true);
}
if (tree != null) {
if (tree.interact("Chop down"))
sleep(random(1000, 1500));
log("Trying to cut tree");
}
break;
case WALK_TO_BANK: // IDE states- Unable to resolve symbol
status = "Walk to Bank";
traversePath(path, true);
sleep(random(1500, 2500));
break;
case WALK_TO_TREE:
status = "Walk to Tree";
if (!myPlayer().isInArea(CHOP_AREA) || client.getInventory().isEmpty() && equipmentTab.isWieldingWeapon(Axe)) {
log("You have an Axe.");
State.WALK_TO_TREE.equals(true);
}
traversePath(path, false);
sleep(random(1500, 2500));
break;
case BANK:
status = "Banking";
RS2Object bank = closestObjectForName("Bank booth");
if (bank != null) {
if (bank.interact("Bank")) {
while (!client.getBank().isOpen())
sleep(250);
client.getBank().depositAll();
}
}
break;
}
return random(200, 300);
}
在CUT案例结束并转到WALK_TO_BANK案例后,IntelliJ表示它无法解析符号。我不太清楚为什么......我对此很陌生,所以我的代码不会像它那样高效,但这只是脚本的一些练习,很明显,我会改进。
答案 0 :(得分:1)
您的语法已关闭。您的case
语句必须位于同一范围内 - else
块之外。
答案 1 :(得分:0)
试试这个:
class Test
{
private enum State {
CUT, WALK_TO_BANK, BANK, WALK_TO_TREE
}
int step = 0;
private State getState()
{
if (step == 1)
return State.WALK_TO_BANK;
if (step == 2)
return State.WALK_TO_TREE;
if (step == 3)
return State.BANK;
return State.CUT;
}
Test()
{
switch (getState())
{
case CUT:
if (true)
{
}
else
{
}
break;
case WALK_TO_BANK: // IDE states- Unable to resolve symbol
break;
case WALK_TO_TREE:
break;
case BANK:
break;
}
}
}