2013-09-25 29 views
-2

目前我有增加商品檢索到的循環

public void sellAllBut() { 
Iterator<String> i = inventory.iterator(); 
while (i.hasNext()) { 
    if (!i.next().equalsIgnoreCase("pickaxe")) { 
    i.remove(); 
    } 
} 

}

現在它不僅能消除從inventory.class字符串數組的項目。我如何添加它,以便當它刪除該項目時,它使用inventory.goldCoins + = intvalue添加黃金數量;

我INT值

//ores 
    int copperoPrice = 150; 
    int ironoPrice = 200; 
    int steeloPrice = 350; 
    int goldoPrice = 500; 
    int diamondoPrice = 700; 
    int pickaxePrice = 500; 

並獲得放入數組中的項目有:字符串值設置爲礦石

String ore = null; 


    if (oreFind <= 50) { 
     ore = "Copper ore"; 

     } 

    if (oreFind >=51) { 
     if (oreFind <= 70) { 
      ore = "Iron ore"; 
     } 
    } 
    if (oreFind >=71) { 
     if (oreFind <= 90) { 
      ore = "Steel ore"; 
     } 
    } 
    if (oreFind >=91) { 
     if (oreFind <= 99) { 
      ore = "Gold ore"; 
     } 
    } 
    if (oreFind == 100) { 
      ore = "Diamond ore"; 
    } 

我使用這行代碼中添加它到我的庫存字符串數組:

inventory.addInventory(ore);

因此,例如我的庫存可以有:金剛石礦,金礦,金礦]

我能做些什麼來給個價每這些並把它放在sellAllBut無效?

回答

0

這有點令人困惑,但不是你只是在做類似的事情;

對於每個礦石價格,創建一個對象,而不是礦石名稱和價格。

然後讓每個對象從一個接口繼承。

現在你有一個集合,你可以搜索使用它的名稱和LINQ。

然後在你的循環中,你可以;

public void sellAllBut() { 
Iterator<String> i = inventory.iterator(); 
while (i.hasNext()) { 
    if (!i.next().equalsIgnoreCase("pickaxe")) { 
    sellAllBut += ores.where(x => x.name == i).firstOrDefault(); 
    i.remove(); 
    } 
} 

以上完全沒有經過測試,但我認爲你正在尋找的方法。讓我知道你是否需要更多,甚至這不是你想要的。

編輯

其實,你並不需要的接口。