用C#减法?

时间:2019-01-11 07:14:16

标签: c#

我无法从c#中的字典中减去项目

这是代码,我正在尝试取得成果。算作一个整数。

public class test
{
    public static void Main()
    {
        int totalStock = 10;`

        Dictionary<string, int> fruits = new Dictionary<string, int>();
        fruits.Add("Apples", 3);
        fruits.Add("pears", 4);
        int fruitsCount = fruits["Apples" + "pears"];
        if(fruitsCount > totalStock){
            Console.WriteLine("You have too many fruits! Please get rid of " + fruitsCount - totalStock + " fruits");
        }
        else if(fruitsCount = totalStock){
            Console.WriteLine("You have just the right amount of fruits!");
        }
        else{
            Console.WriteLine("You can fit " + totalStock - fruitsCount + " fruits");
        }

    }
}

但是我遇到了错误:

  

退出状态1 main.cs(14,21):错误CS0019:运算符“-”不能为   应用于类型为'string'和'int'

的操作数      

main.cs(16,10):错误   CS0029:无法将类型'int'隐式转换为'bool'

     

main.cs(20,21):   错误CS0019:运算符'-'不能应用于类型的操作数   'string'和'int'

7 个答案:

答案 0 :(得分:2)

没有 "Apples" + "pears" == "Applespears"水果,这就是为什么

fruits["Apples" + "pears"];

是错误的。将其表示为fruits["Apples"] + fruits["pears"];

public static void Main() {
  int totalStock = 10;

  Dictionary<string, int> fruits = new Dictionary<string, int>() {
    {"Apples", 3},       
    {"pears", 4},
  };

  // Either Apples + pears
  int fruitsCount = fruits["Apples"] + fruits["pears"];
  // ... Or sum of all the fruits
  // int fruitsCount = fruits.Values.Sum();

  if (fruitsCount > totalStock){
    Console.WriteLine($"You have too many fruits! Please get rid of {fruitsCount - totalStock} fruits");
  }
  else if(fruitsCount == totalStock) { // should be comparison "==" not assignement "="
    Console.WriteLine("You have just the right amount of fruits!");
  }
  else {
    Console.WriteLine($"You can fit {totalStock - fruitsCount} fruits");
  }

请注意string s:我们不能减去string,而是整数。在您的情况下,string interpolation是解决方案(我们在字符串中减去 integers ):

  $"You have too many fruits! Please get rid of {fruitsCount - totalStock} fruits"

  $"You can fit {totalStock - fruitsCount} fruits"

答案 1 :(得分:1)

int fruitsCount = fruits["Apples" + "pears"];

是错误的。 您可以通过以下方式访问字典值:

int fruitsCount = fruits["Apples"] + fruits["pears"];

答案 2 :(得分:1)

int fruitsCount = fruits["Apples" + "pears"];

无效的C#。您可以使用

int fruitsCount = fruits["Apples"] + fruits["pears"];

或者如果您想使用LINQ

int fruitsCount = fruits.Values.Sum()

else if(fruitsCount = totalStock){ 

应该是

else if(fruitsCount == totalStock){

否则,您正在执行if条件下无法执行的任务。

要使您的最后一个减法正确,您需要

Console.WriteLine($"You can fit { totalStock - fruitsCount } fruits");

答案 3 :(得分:1)

尝试

 Console.WriteLine($"You can fit {totalStock - fruitsCount} fruits");

答案 4 :(得分:0)

您有两个主要错误。

  1. fruits["Apples" + "pears"]的值为fruits["Applespears"]。您可能是说fruits["Apples"] + fruits["pears"]

  2. ==分支中使用=代替else if进行相等比较。

完整代码:

int totalStock = 10;`

Dictionary<string, int> fruits = new Dictionary<string, int>();
fruits.Add("Apples", 3);
fruits.Add("pears", 4);
int fruitsCount = fruits["Apples"] + fruits["pears"];
if(fruitsCount > totalStock){
    // note the added brackets
    Console.WriteLine("You have too many fruits! Please get rid of " + (fruitsCount - totalStock) + " fruits");
}
else if(fruitsCount = totalStock){
    Console.WriteLine("You have just the right amount of fruits!");
}
else{
    Console.WriteLine("You can fit " + (totalStock - fruitsCount) + " fruits");
}

答案 5 :(得分:0)

尝试

   static void Main(string[] args)
        {
            int totalStock = 10;
            Dictionary<string, int> fruits = new Dictionary<string, int>();
            fruits.Add("Apples", 3);
            fruits.Add("pears", 4);
            int fruitsCount = fruits["Apples"]+ fruits["pears"];
            if (fruitsCount > totalStock)
            {
                Console.WriteLine($"You have too many fruits! Please get rid of {fruitsCount - totalStock} fruits");
            }
            else if (fruitsCount == totalStock)
            {
                Console.WriteLine("You have just the right amount of fruits!");
            }
            else
            {
                Console.WriteLine($"You can fit { totalStock - fruitsCount } fruits");
            }

        }

答案 6 :(得分:0)

首先,您应该了解有关Dictionary的更多信息,您可以找到一个很好的信息等级:https://www.dotnetperls.com/dictionary

我曾使用list来获取水果整数的所有值,如fruitsValuesCount。比起清单fruitsValuesSum

您可以找到正确的答案:

using System;
using System.Collections.Generic;
using System.Linq;
namespace Test01
{
    class Program
    {
        static void Main(string[] args)
        {
            int totalStock = 10;

            var fruits = new Dictionary<string, int>
            {
                {"Apples", 3},
                {"pears", 4}
            };
            var fruitsValuesCount = new List<int>(fruits.Values);
            var fruitsValuesSum = fruitsValuesCount.Sum();
            int totalFruits = totalStock - fruitsValuesSum;
            if (fruitsValuesSum > totalStock)
            {
                Console.WriteLine("You have too many fruits! Please get rid of " + totalFruits + " fruits");
            }
            else if (fruitsValuesSum == totalStock)
            {
                Console.WriteLine("You have just the right amount of fruits!");
            }
            else
            {
                Console.WriteLine("You can fit " + totalFruits + " fruits");
            }

        }
    }
}