【问题标题】:Subtraction in c#?c#中的减法?
【发布时间】:2019-01-11 07:14:16
【问题描述】:

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

这是代码,我正在尝试获取 fruits.Count 作为 int。

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): error CS0019: Operator `-' cannot be 应用于'string'和'int'类型的操作数

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

main.cs(20,21): 错误 CS0019:运算符“-”不能应用于类型的操作数 “字符串”和“整数”

【问题讨论】:

  • elseif 似乎也有问题 - 您尝试 assing fruitStock = totalStock 而不是比较它们。
  • 字典由键访问,在您的情况下是string 类型。 Int 这一行 int fruitsCount = fruits["Apples" + "pears"] 您正在连接两个字符串,因此您正在尝试访问名为 fruits["Applespears"] 的产品,该产品不在字典中。改成int fruitsCount = fruits["Apples"] + fruits["pears"]
  • c#中的相等比较器是==而不是=
  • Operator '-' cannot be applied to operands of type 'string' and 'int'”是因为 "... rid of " + fruitsCount - totalStock + " fruits" 你必须手动进行一些类型转换或使用新变量,我会 int excessOfFruit = fruitsCount - totalStock 并在 Console.WriteLine 指令中使用该变量。

标签: c#


【解决方案1】:

没有 "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");
  }

小心strings:我们不能减去string,而是整数;在您的情况下,string interpolation 是解决方案(我们在字符串中减去 整数):

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

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

【讨论】:

    【解决方案2】:
    int fruitsCount = fruits["Apples" + "pears"];
    

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

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

    【讨论】:

    • 错了,但这不是 OP 发布的问题的原因。
    【解决方案3】:

    试试

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

    【讨论】:

    • 我建议添加错误的原因并使其完整、完美,添加一些有关此代码如何/为什么解决问题的信息。
    【解决方案4】:
    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");
    

    【讨论】:

    • 无效,但这不是 OP 发布的问题的原因。
    • OP 有 3 个不同的错误,这是其中之一。确实是部分答案,但它修复了部分错误。
    【解决方案5】:

    您有两个主要错误。

    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");
    }
    

    【讨论】:

    • 实际上有 3 个错误。您的代码还修复了“运算符'-'不能应用于'string'和'int'类型的操作数”但忘了提及它。
    • @bradbury9 我确实在代码的 cmets 中提到了它。我认为这是一个相当小的问题,所以我没有说这是一个“主要”错误。
    • OP 对 c# 来说似乎很新,可能会想“为什么需要括号?”
    【解决方案6】:

    试试这个

       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");
                }
    
            }
    

    【讨论】:

      【解决方案7】:

      首先你应该了解更多关于字典的信息,你可以找到一个很好的信息hier: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");
                  }
      
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-28
        • 2012-04-20
        相关资源
        最近更新 更多