【问题标题】:Cannot implicitly convert type `string' to `System.Collections.Generic.List<int>'无法将类型“字符串”隐式转换为“System.Collections.Generic.List<int>”
【发布时间】:2019-08-23 20:33:58
【问题描述】:

这应该比较每个列表中的三个数字,如果 A 中的数字大于 B 中的数字,则指向 C,如果 B 中的数字大于 A 中的数字,则指向 D,并且将如果他们是平等的,什么也不做。这是一个例子:

Input     | output
A= 1 3 2  |  C D
B= 3 2 2  |  1 1

代码如下:

static List<int> compareTriplets(List<int> a, List<int> b)
{
    int c = 0;
    int d = 0;
    for (int i = 0; i < 3; i++)
    {
        if (a[i] > b[i])
        {
            c++;
        }
        else if (b[i] > a[i])
        {
            d++;
        }
    }
    return c + " " + d;
}

这段代码返回给我这个:

错误 CS0029:无法将类型“字符串”隐式转换为“System.Collections.Generic.List

【问题讨论】:

  • 您应该返回List&lt;int&gt;,但您返回的是c +" "+ d。我不明白问题的其余部分。
  • 尝试返回 c[i].ToString() +" "+ d[i].ToString;
  • @JuniorCortenbach:那行不通。它仍然是一个字符串
  • 也许返回签名应该是string
  • 花点时间想想你在做什么。该函数应该返回什么?它应该返回一个字符串吗?还是应该返回一个包含两个值的列表?

标签: c#


【解决方案1】:

对我来说,这段代码看起来像是要返回 2 个数字:

return c + " " + d;

但是这样会变成一个字符串,死不匹配方法签名。

要返回数字列表(例如 2 个数字),您可以使用

return new List<int>{c, d};

【讨论】:

    【解决方案2】:

    您正在尝试将字符串转换为列表。函数compareTriplets 必须返回一个字符串。

    一个建议:你需要在使用索引进行迭代时检查列表的边界。

    static string compareTriplets(List<int> a, List<int> b)
    {
        int c = 0;
        int d = 0;
        for (int i = 0; i < 3; i++)
        {
            if (a[i] > b[i])
                c++;
            else if (b[i] > a[i])
                d++;
        }
        return c + " " + d;
    }
    

    使用 C# 6.0+,你可以这样返回:

    return $"{c} {d}";
    

    【讨论】:

    • 您在return 中将整数类型连接到字符串类型。
    • 字符串被隐式转换@JoshuaSchlichting
    • @JoshuaSchlichting:它将编译并运行。 Try it yourself.
    • 有趣。我认为那里的演员阵容是合适的。
    【解决方案3】:

    你的返回类型是string:a + " " + b,所以你需要更改方法签名:

    static string compareTriplets(List<int> a, List<int> b) {
        // etc....
    

    【讨论】:

      【解决方案4】:

      您应该返回类型List&lt;int&gt;,但您返回的是c +" "+ d。我不明白问题的其余部分。

      您应该返回一个列表。这是一个例子:

      static List<int> compareTriplets(List<int> a, List<int> b) 
      {
          int c = 0;
          int d = 0;
      
          for (int i=0;i<3;i++)
              if (a[i]>b[i])
                  c++;
              else if (b[i]>a[i])
                  d++;
      
          return new List<int>{ c, d };
       }
      

      【讨论】:

        【解决方案5】:

        您指定您的函数将返回一个整数列表:static List&lt;int&gt;。您需要将其更改为字符串。

        static String compareTriplets(List<int> a, List<int> b) {
            int c = 0;
            int d = 0;
            for (int i=0;i<3;i++){
                if (a[i]>b[i]){
                   c++;
                }
                else if (b[i]>a[i])
                {
                    d++;
                }
            }
            return c.ToString() + " " + d.ToString();
         }
        

        编辑:不需要显式 ToString() 演员表。

        如果您真的想返回 List&lt;int&gt;,请执行以下操作:

        static List<int> compareTriplets(List<int> a, List<int> b) {
            int c = 0;
            int d = 0;
            for (int i=0;i<3;i++){
                if (a[i]>b[i]){
                   c++;
                }
                else if (b[i]>a[i])
                {
                    d++;
                }
            }
            return new List<int>(){ c, d };
         }
        

        【讨论】:

        • 您无需致电ToString,它会自动为您完成。这适用于每个object,而不仅仅是int
        • 我的错!我更喜欢显式,但我没有意识到 int 没有 .toString()!
        • 它有一个ToString(),上面有一个T。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-17
        • 1970-01-01
        • 2015-01-15
        • 2019-12-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多