2016-10-13 21 views
0

的部分當我使用||時,布爾運算結果爲FALSE OR或&&和JAVA

static boolean isBagFull = ((!Bag.itemSlot1.equals("Empty"))||(!Bag.itemSlot2.equals("Empty"))||(!Bag.itemSlot3.equals("Empty"))||(!Bag.itemSlot4.equals("Empty"))||(!Bag.itemSlot5.equals("Empty"))); 

總是變成假的!請幫忙!雖然我檢查了所有的itemSlots,他們都是 「空」

static Bag bag = new Bag(); 
static String helmet = ""; 
static String chestplate = ""; 
static String leggings = ""; 
static String boots = ""; 
static boolean isBagFull = ((!Bag.itemSlot1.equals("Empty"))||(!Bag.itemSlot2.equals("Empty"))||(!Bag.itemSlot3.equals("Empty"))||(!Bag.itemSlot4.equals("Empty"))||(!Bag.itemSlot5.equals("Empty"))); 


public void removeArmour(String s, int b){ 
    if (b==1&&!helmet.equals("")) { 
     if (!isBagFull){//ITS USED HERE GUYS!!!!!!!!!!!!!!!!!! HELP!!!!! 
      bag.newItem(s); 
      helmet = ""; 
     }else{ 
      JOptionPane.showMessageDialog(null, "You do not have any room to remove the "+s); 
     } 
    }else if (b==2&&!chestplate.equals("")){ 
     if (!isBagFull){ 
      bag.newItem(s); 
      chestplate = ""; 
     }else{ 
      JOptionPane.showMessageDialog(null, "You do not have any room to remove the "+s); 
     } 
    }else if (b==3&&!leggings.equals("")){ 
     if (!isBagFull){ 
      bag.newItem(s); 
      leggings = ""; 
     }else{ 
      JOptionPane.showMessageDialog(null, "You do not have any room to remove the "+s); 
     } 
    }else if (b==4&&!boots.equals("")){ 
     if (!isBagFull){ 
      bag.newItem(s); 
      boots = ""; 
     }else{ 
      JOptionPane.showMessageDialog(null, "You do not have any room to remove the "+s); 
     } 
    }else if (b>=5||b<=0){ 
     JOptionPane.showMessageDialog(null, "Sorry you don't have the correct position!", "ERROR", 0); 
     JOptionPane.showMessageDialog(null, "Helmet = 1 \n Chestplate = 2 \n Leggings = 3 \n Boots = 4", "Armour Numbers", 0); 
    }else{ 
     JOptionPane.showMessageDialog(null, "You ar not wearing anything there!"); 
    } 
} 
+1

你需要將代碼發佈到Bag類,這可能比這裏的任何事情都要多得多。 – Tibrogargan

+0

請學習使用數組。任何時候你有像'itemSlot1','itemSlot2','itemSlot3'這樣的變量,這就是你需要使用一個數組來代替所有變量分開的標誌。 – ajb

+0

是的,我在這篇文章後纔剛剛開始做這個工作!謝謝!! @ajb –

回答

1

由於所有itemSlots是 「空」 的條件:

((!Bag.itemSlot1.equals("Empty")) 
||(!Bag.itemSlot2.equals("Empty")) 
||(!Bag.itemSlot3.equals("Empty")) 
||(!Bag.itemSlot4.equals("Empty")) 
||(!Bag.itemSlot5.equals("Empty"))); 

計算結果爲:

((!"Empty".equals("Empty")) 
||(!"Empty".equals("Empty")) 
||(!"Empty".equals("Empty")) 
||(!"Empty".equals("Empty")) 
||(!"Empty".equals("Empty"))); 

計算結果爲:

((!true) 
||(!true) 
||(!true) 
||(!true) 
||(!true)); 

它應該是顯而易見的,爲什麼它總是假:)

假設你的包是完全如果任何一個插槽是不是空的,你真的想:

static boolean isBagFull = ((Bag.itemSlot1.equals("Empty"))||(Bag.itemSlot2.equals("Empty"))||(Bag.itemSlot3.equals("Empty"))||(Bag.itemSlot4.equals("Empty"))||(Bag.itemSlot5.equals("Empty"))); 

但如果它是唯一的全方位如果所有的插槽不爲空,你想要:

static boolean isBagFull = ((Bag.itemSlot1.equals("Empty"))&&(Bag.itemSlot2.equals("Empty"))&&(Bag.itemSlot3.equals("Empty"))&&(Bag.itemSlot4.equals("Empty"))&&(Bag.itemSlot5.equals("Empty"))); 
+0

噢!這就說得通了! –