繁体   English   中英

从double到int的可能有损转换

[英]Possible lossy conversion from double to int

为什么我得到Possible lossy conversion from double to int错误,我该如何解决?

public class BinSearch {
    public static void main(String [] args)
    {
        double set[] = {-3,10,5,24,45.3,10.5};
        double l = set.length;
        double i, j, first, temp;
        System.out.print("Before it can be searched, this set of numbers must be sorted: ");
        for (i = l-1; i>0; i--)
        {
            first=0;
            for(j=1; j<=i; j++)
            {
                if(set[j] < set[first]) // location of error according to compiler
                {
                    first = j;
                }
                temp = set[first];
                set[first] = set[i];
                set[i] = temp;
            }
        }
    } 
}

正如你所看到的,我已经尝试过更换intdouble声明变量时接近顶部,但它似乎并没有做的工作。

将用作数组索引的所有变量从double更改为int(即变量jfirsti )。 数组索引是整数。

数组/循环索引应该是整数,而不是双精度数。

例如,当试图访问set[j] ,它抱怨将j视为int。

更改变量类型如下。 数组索引必须是int类型。

public class BinSearch {
      public static void main(String [] args)
      {
          double set[] = {-3,10,5,24,45.3,10.5};
          int l = set.length;
          double temp;
          int i, j, first;
          System.out.print("Before it can be searched, this set of numbers must be sorted: ");
          for ( i = l-1; i>0; i--)
          {
              first=0;
              for(j=1; j<=i; j++)
          {
              if(set[j] < set[first])//location of error according to compiler
              {
                  first = j;
              }
              temp = set[first];
              set[first] = set[i];
              set[i] = temp;
          }
      }
  } 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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