简体   繁体   English

C#如何根据字典的键将字典的值添加到变量中?

[英]C# How do I add the Value of a Dictionary to a variable based on its Key?

I have to calculate the price of an order based on the input from the user using a Dictionary, but I can't seem to get it right. 我必须根据用户使用Dictionary的输入来计算订单价格,但我似乎无法正确理解它。

  1. Chicken Strips - $3.50 鸡条-$ 3.50
  2. French Fries - $2.50 炸薯条-2.50美元
  3. Hamburger - $4.00 汉堡包-$ 4.00
  4. Hotdog - $3.50 热狗-$ 3.50
  5. Large Drink - $1.75 大型饮料-1.75美元
  6. Medium Drink - $1.50 中度饮料-1.50美元
  7. Milk Shake - $2.25 奶昔-$ 2.25
  8. Salad - $3.75 沙拉-3.75美元
  9. Small Drink - $1.25 小饮料-1.25美元

For example, if the input is 236, the price of the order should be $8. 例如,如果输入为236,则订单价格应为$ 8。

Converting my LINQ query to double didn't work (my query is also definitely wrong). 将我的LINQ查询转换为double无效(我的查询也肯定是错误的)。 I don't really know any LINQ but as you can't really use a for loop for a dictionary as they aren't indexed, I don't really have any other choice. 我真的不知道任何LINQ,但是由于您不能为字典真正使用for循环,因为它们没有被索引,所以我真的没有其他选择。

        Dictionary<int, double> items = new Dictionary<int, double>()
        {
            { 1, 3.50 },
            { 2, 2.50 },
            { 3, 4.00 },
            { 4, 3.50 },
            { 5, 1.75 },
            { 6, 1.50 },
            { 7, 2.25 },
            { 8, 3.75 },
            { 9, 1.25 }
        };

        string order = Console.ReadLine();
        double sum = 0;
        for (int i = 0; i < order.Length; i++)
        {
            sum += Convert.ToDouble(items.Where(x => x.Key == int.Parse(order[i].ToString())).Select(x => x.Value));
        }
        Console.WriteLine(sum);
        Console.ReadKey();

Well, the output should be the correct amount of dollars based on the input from the user. 嗯,输出应该是基于用户输入的正确金额。

无需循环。

var sum = order.Sum(c => items[int.Parse(c.ToString())]);

This implementation is quite fragile, it only allows 10 items and relies on string parsing but I think the following code does what you're asking: 此实现非常脆弱,它仅允许10个项目并且依赖于字符串解析,但是我认为以下代码可以满足您的要求:

order.Sum(character =>
{
    int id = character - '0';

    return items.TryGetValue(id, out double price) ? price : 0d;
});

TryGetValue checks that order only contains digits which are keys in the items dictionary. TryGetValue检查order仅包含数字,这些数字是项字典中的键。

尝试这个

order.Select(c => (c - '0')).Where(c => c > 0 && c < 10).Sum(c => items[c])

You can use sum += items[order[i]-'0']; 您可以使用sum += items[order[i]-'0']; in your for loop. 在您的for循环中。

This works because '0' is stored with the value 48 (see the answers to this question ). 之所以有效,是因为'0'存储为值48(请参阅此问题的答案)。 The rest of the digits are stored in order ('1' is 49, '2' is 50, etc), so '1' - '0' == 1 and so forth. 其余数字按顺序存储(“ 1”为49,“ 2”为50,依此类推),因此“ 1”-“ 0” == 1。依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据C#.NET Silverlight中Dictionary中的键值对字典列表进行排序? - How do I sort a List of Dictionaries based off of a value in a key within the Dictionary in C# .NET Silverlight? 如何将字典键和值作为字符串C#分配给变量 - How to assign a dictionary key and value to a variable as a string C# 如何从包含值元组作为 C# 键的字典中提取值? - How do I extract a value out of a dictionary that contains a value tuple as the key in C#? 当值为 Object 时,如何在 Unity 中使用 C# 字典通过值查找键 - How do I find key through value with C# dictionary in Unity when value is an Object 如何动态地添加和命名键值对到字典? C# - How to add and name key–value pairs to a dictionary dynamically? C# 在 C# 字典中添加和递增键和值 - Add & Increment the Key and Value in C# dictionary c#如果字典中存在键仍然在字典中添加值 - c# if key exist in dictionary still add the value in dictionary C#将元素添加到Dictionary,键为字符串,值为另一个词典 - C# Add elements to Dictionary with key as string and value as another Dictionary c#检查密钥是否存在于字典中,然后传递其值 - c# check if key exists in dictionary then pass on its value 根据值获取 C# 字典中键/值对的索引 - Get index of a key/value pair in a C# dictionary based on the value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM