2011-11-29 37 views
0

我有一個小問題,在我的源代碼我不明白爲什麼我的ToUpload方法給我一個額外的console.writeLine在控制檯窗口。 (寫出啤酒對象)C#方法給我一個額外的Console.WriteLine

當我調用ToUpload方法並且啤酒在啤酒字典中時,它給了我一個額外的Console.WriteLine,其中在控制檯處寫出啤酒對象。我不知道爲什麼。

這是我的輸出:

  • Borsodi SOR 160 4.6 1000
  • 比爾森250 4.4 800
  • SoproniÁszok150 4.5 900
  • 德雷爾經典200,5.2 600
  • Borsodisör160 4.6 475
  • 比爾森250 4.4 800
  • SoproniÁszok150 4.5 1350
  • 德雷爾經典200,5.2 600
  • Bratista SOR,230,300 4.5
  • SoproniÁszok150 4.5 450 //這是額外
  • Borsodi SOR 160 4.6 100 //這是

,我想這樣:

  • Borsodi SOR 160 4.6 1000
  • 比爾森250 4.4 800
  • SoproniÁszok150 4.5 900
  • 德雷爾經典200,5.2 600
  • Borsodi SOR 160 4.6 475
  • 比爾森Urquell 250 4.4 800
  • SoproniÁszok150,4.5 1350
  • Dreher Classic 200,5.2 600
  • Bratista SOR,230,300 4.5

    public void ToUpload(Beer beer, int dl) 
    { 
        int d = 0; 
        Beer s = null; 
        // search for beer in beers dictionary 
        foreach (var item in beers) 
        { 
         if (item.Key.Equals(beer)) 
         { 
          d = item.Value; 
          s = item.Key; 
         } 
        } 
        // if this beer in the beers, update the value 
        if (s != null) 
        { 
         beers[s] = d + dl; 
        } 
        // if a new beer, just add to beers 
        beers.Add(beer, dl); // IDictionary beers = new Dictionary 
    } 
    
    public Pub() 
    { 
        ToUpload(new Beer("Borsodi beer", 160, 4.6), 1000); 
        ToUpload(new Beer("Pilsner Urquell", 250, 4.4), 800); 
        ToUpload(new Beer("Soproni Ászok", 150, 4.5), 900); 
        ToUpload(new Beer("Dreher Classic", 200, 5.2), 600); 
    } 
    
    static void Main(String[] args) 
    { 
        Beer b = new Beer("Borsodi beer", 160, 4.6); 
        Beer c = new Beer("Bratista beer", 230, 4.5); 
        Beer d = new Beer("Soproni Ászok", 150, 4.5); 
        Pub pub = new Pub(); 
        foreach (var item in pub.beers) 
        { 
         Console.WriteLine("{0} {1}", item.Key, item.Value); 
    
        } 
    
    
        Console.WriteLine(pub.Elad("Borsodi beer", 125)); 
    
    
        //pub.ToUpload(b, 2000); 
        pub.ToUpload(c, 300); // Don't Write out this beer object 
        pub.ToUpload(d, 450); // Write out this beer object the console 
        pub.ToUpload(b, 100); // Write out this beer object the console 
    
    
    
        foreach (var item in pub.beers) 
        { 
         Console.WriteLine("{0} {1}", item.Key, item.Value); 
    
        } 
    
        Console.ReadLine(); 
    

下面是啤酒類:

public class Beer 
{ 
    string name; 
    int price; 
double alcohol; 

public string Name { get { return name; } } 

public int Price{ get; set; } 

public double AlkoholTartalom { get { return alcohol; } } 

public Beer(string name, int price, double alcohol) 
{ 
    // ide írja a kódot 
    this.name = name; 
    this.price = price; 
    this.alcohol = alcohol; 
} 

public override bool Equals(object obj) 
{ 
    if (obj is Beer) 
    { 
     Beer other = (Beer)obj; 
     return this.name == other.name; 
    } 
    return false; 
} 

public override string ToString() 
{ 
    return this.Name + " " + this.Price+ " " + this.AlkoholTartalom; 
} 
} 

回答

2

在ToUpload方法你缺少 「其他」。

// if this beer in the beers, update the value 
if (s != null) 
{ 
    beers[s] = d + dl; 
} 
else // Add this 
    beers.Add(beer, dl); // IDictionary beers = new Dictionary 

從我所看到的,這應該修復它。 你正在添加啤酒到字典,即使你只是更新它。

+0

謝謝。我需要睡覺:D – blaces

+0

嘿,沒問題。 – Katu

0

根據什麼類型的啤酒是你可以這樣做:

public void ToUpload(Beer beer, int dl) 
{ 
    int d = 0; 
    if(beers[beer] != null) 
     d = beers[beer]; 

    beers[beer] = d + dl; 
} 

我知道哈希表會自動更新或添加到基於該鍵是否字典,發現當他們訪問該辦法。

相關問題