2014-05-16 42 views
-2

我一直在練習Java編程,最近以爲我會試着使用不同的庫來嘗試一些基於狀態的腳本。我用一個switch語句碰到了一個障礙,我似乎無法進入第二種情況。現在我明白你們可能不熟悉外部API,但我只是想知道我的邏輯是否有問題。該國來自一個枚舉和我有不同的先決條件測試一個的getState()方法:爲什麼此Java Switch語句無法進展?

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語句:

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表明它無法解析該符號。我不太清楚爲什麼......我對此很新,所以我的代碼效率不會如此高,但這只是腳本編寫的一些練習,很明顯,我會改進它。

+3

你錯過了梅開二度'}'收了'else'聲明'的'break'之前應該去CUT'情況。 –

+0

這實際上使得代碼可以運行,我錯過了其中的2個:/。謝謝您的幫助。 – user3646185

+0

不客氣。 –

回答

1

您的語法關閉。您的case陳述都必須在相同的範圍內 - else區塊之外。

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; 
     } 
    } 

}