C# 方法给我一个额外的控制台

C# 方法给我一个额外的控制台,c#,methods,console,C#,Methods,Console,我有一个小问题,在我的源代码中,我不明白为什么我的ToUpload方法会在控制台窗口中给我一个额外的console.writeLine。(写出啤酒对象) 当我调用ToUpload方法时,啤酒在啤酒字典中,它会给我一个额外的Console.WriteLine,在控制台中写出啤酒对象。我不知道为什么 这是我的输出: Borsodi sör 160 4.6 1000 皮尔斯纳·厄克尔250 4.4 800 SoproniÁszok 150,4.5900 德雷尔经典200,5.2600 125 Bor

我有一个小问题,在我的源代码中,我不明白为什么我的ToUpload方法会在控制台窗口中给我一个额外的console.writeLine。(写出啤酒对象)

当我调用ToUpload方法时,啤酒在啤酒字典中,它会给我一个额外的Console.WriteLine,在控制台中写出啤酒对象。我不知道为什么

这是我的输出:

  • Borsodi sör 160 4.6 1000
  • 皮尔斯纳·厄克尔250 4.4 800
  • SoproniÁszok 150,4.5900
  • 德雷尔经典200,5.2600
  • 125
  • Borsodi sör 160 4.6 475
  • 皮尔斯纳·厄克尔250 4.4 800
  • SoproniÁszok 150,4.5 1350
  • 德雷尔经典200,5.2600
  • Bratista sör,230,4.5300
  • SoproniÁszok 150,4.5 450//这是额外的
  • Borsodi sör 160 4.6 100//这是
我想要这个:

  • Borsodi sör 160 4.6 1000
  • 皮尔斯纳·厄克尔250 4.4 800
  • SoproniÁszok 150,4.5900
  • 德雷尔经典200,5.2600
  • 125
  • Borsodi sör 160 4.6 475
  • 皮尔斯纳·厄克尔250 4.4 800
  • SoproniÁszok 150,4.5 1350
  • 德雷尔经典200,5.2600
  • Bratista sör,230,4.5300

    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;
}
}
在ToUpload方法中缺少“Else”

// 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
从我看来,这应该可以解决它。
您正在将啤酒添加到词典中,即使是您也应该更新它。

根据啤酒的类型,您可以执行以下操作:

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

    beers[beer] = d + dl;
}
我知道哈希表将根据在以这种方式访问时是否找到密钥自动更新或添加到字典中