简体   繁体   中英

Subtraction in c#?

I cannot subtract items form my dictionary in c#

This is the code, i am trying to get the fruits.Count as an 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");
        }

    }
}

But im getting errors:

exit status 1 main.cs(14,21): error CS0019: Operator `-' cannot be applied to operands of type 'string' and 'int'

main.cs(16,10): error CS0029: Cannot implicitly convert type 'int' to 'bool'

main.cs(20,21): error CS0019: Operator '-' cannot be applied to operands of type 'string' and 'int'

You don't have "Apples" + "pears" == "Applespears" fruit, that's why

fruits["Apples" + "pears"];

is wrong. Put it as 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");
  }

Be careful with string s: we can't subtract string but integers; in your case string interpolation is the solution (we subtract integers within the string):

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

  $"You can fit {totalStock - fruitsCount} fruits"
int fruitsCount = fruits["Apples" + "pears"];

is wrong. You can access dictionary values by writing:

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

is not valid C#. You can use

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

or if you want to use LINQ

int fruitsCount = fruits.Values.Sum()

else if(fruitsCount = totalStock){ 

should be

else if(fruitsCount == totalStock){

otherwise you are doing an assignment which you can not do in a if condition.

To make your last subtraction right you need

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

尝试

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

You have two main errors.

  1. fruits["Apples" + "pears"] evaluates to fruits["Applespears"] . You probably meant fruits["Apples"] + fruits["pears"] .

  2. Use == instead of = for equality comparison in the else if branch.

Full code:

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

Try this

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

        }

Frist you should learn more about Dictionary, you can find a good info hier: https://www.dotnetperls.com/dictionary

I had used list to get all the values of the integer of the fruits as fruitsValuesCount . Than i put the list in sum as fruitsValuesSum

Hier you can find the right answer:

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

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM