【问题标题】:Cannot implicitly convert type double to int无法将类型 double 隐式转换为 int
【发布时间】:2019-08-18 09:09:05
【问题描述】:

所以我有一堆数组,我正在尝试使用ArrayA 的索引从arrayB 中检索一个元素。例如。如果我有名字cucumber 并且它在arrayB 中,我正在尝试从另一个数组中获取cucumber 的索引。

如下代码所示,我的代码的最后一行不起作用,它告诉我不能将 double 转换为 int。

编辑:我将代码更改为int index,现在我的fmin(something1, something2) 给了我同样的错误...

    private double fmin(double[] a, int b)
    {
        double Min = a[0];
        for (int i = 0; i < b; i++)
        {
            if (Min > a[i])
                Min = a[i];
        }
        Min = Array.IndexOf(a, min);
        return Min;
    }

    int index = fmin(something1, something2);
    output = (something3[index]);

【问题讨论】:

  • 因为 fmin 返回一个索引,你应该使用 int 作为返回类型。那么你的代码应该可以工作了。
  • 将您的 Min 转换为 int:Convert.ToInt32(Min) 并且您的方法返回类型应为 private int fmin(double[] a, int b)
  • something2 变量的数据类型是什么?这是编译器错误而不是运行时错误。
  • 今天将是摆脱使用缩写词的习惯的好日子。它 mks it hrd 2 rd yr cd。调用你的方法MinimumValue 或描述其目的的一些类似的东西。
  • 另外,这说明违反了编写清晰代码的基本原则:每个变量都应该有一个目的。您使用min 来表示“最小值”和“最小值的位置”,这种混淆导致了您的错误。创建第二个变量 int 并且是索引。

标签: c# arrays min strong-typing


【解决方案1】:

最简单的解决方法是从您的函数中返回 int。

private int fmin(double[] a, int b)
{
    double Min = a[0];
    for (int i = 0; i < b; i++)
    {
        if (Min > a[i])
            Min = a[i];
    }

    return Array.IndexOf(a, Min);
}

int index = fmin(something1, something2);
output = (something3[index]);

解决相同问题的另一种方法是在遍历数组时跟踪最小值的索引。

【讨论】:

    【解决方案2】:

    fmin 返回 Min,它是 double 类型。然后设置索引,它是 fmin(double) 的返回值的 int 类型。使变量类型一致

    【讨论】:

      【解决方案3】:

      首先,作为一种良好做法,我建议习惯于在每个算法开始时进行一些错误情况检查,以确保完全覆盖您的输入。

      其次,正如 Eric Lippert 所说,您必须为变量分配有意义的名称,并避免在用途不同时重复使用它们,除非您过度使用优化一些经常在循环中运行的算法,并进行大量迭代。

      您还可以利用输出变量的强大功能:必须在函数返回之前设置它们。在异常情况下,该函数没有义务为输出变量设置任何值。所以你会有一个很好的函数,它从arrayA返回minIndex和minValue。

      如果您需要一个永不抛出异常的“稳定”函数,我已经编写了另一个实现“bool TryDoSomenthing(out result)”模式的版本。此函数仅在索引计算成功的情况下返回“true”。我在异常不理想的系统上广泛使用这种模式,因为在多个顺序失败的情况下它们会影响性能。

      代码:

      // Finds the index of the lowest value in the array, considering just the 'n' first numbers.
      // Throws exceptions in case of bad input parameters.
      private static void GetMin(double[] a, int n, out int minIndex, out double minValue)
      {
          if (a == null)
              throw new ArgumentNullException(nameof(a));
          if (a.Length <= 0)
              throw new ArgumentException("array 'a' contains no elements");
          if (n <= 0)
              return new ArgumentException("'n' must be greater than 0");
      
          minIndex = 0;
          minValue = a[0];
          if (n > a.Length)
              n = a.Length;
          for (i = 1; i < n; ++i)
              if (minValue > a[i])
                  minValue = a[(minIndex = i)];
      }
      
      // Finds the index of the lowest value in the array, considering just the 'n' first numbers.
      // Returns 'false' in case of bad input parameters.
      private static bool TryGetMin(double[] a, int n, out int minIndex, out double minValue)
      {
          if (a == null || a.Length <= 0 || n <= 0)
          {
              minIndex = -1;
              minValue = double.MinValue;
              return false;
          }
      
          minIndex = 0;
          minValue = a[0];
          if (n > a.Length)
              n = a.Length;
          for (i = 1; i < n; ++i)
              if (minValue > a[i])
                  minValue = a[(minIndex = i)];
          return true;
      }
      
      
      internal static void Main()
      {
          double[] arrayA = new double[] { 3.0, 2.0, 4.0, 1.0 };
          object[] arrayB = new object[] { 'x', 'y', 'z', 'w' };
      
          int minIndex; // index of minimum value found in 'arrayA'
          double minValue; // minimum value found in 'arrayA'
          object b; // used for storing the element of arrayB at 'minIndex'
      
          // With GetMin() - this code will throw an exception on bad inputs:
          GetMin(arrayA, 3, out minIndex, out minValue);
          b = arrayB[minIndex];
      
          // Or with TryGetMin:
          if(TryGetMin(arrayA, 3, out minIndex, out minValue))
          {
              b = arrayB[minIndex];
          }
          else
          {
              // TryGetMin() was not successful, do something about it.
              b = null;
          }
      
          // Or simply:
          b = TryGetMin(arrayA, 3, out minIndex, out minValue) ? arrayB[minIndex] : null;
      
          //...
      }
      

      【讨论】:

        猜你喜欢
        • 2020-05-03
        • 1970-01-01
        • 2014-01-07
        • 1970-01-01
        • 1970-01-01
        • 2017-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多