简体   繁体   English

从double到int的可能有损转换

[英]Possible lossy conversion from double to int

Why am I getting the Possible lossy conversion from double to int error and how can I fix it? 为什么我得到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;
            }
        }
    } 
}

As you can see, I've already tried replacing int with double near the top when declaring variables but it doesn't seem to do the job. 正如你所看到的,我已经尝试过更换intdouble声明变量时接近顶部,但它似乎并没有做的工作。

Change all your variables used as array indices from double to int (ie the variables j , first , i ). 将用作数组索引的所有变量从double更改为int(即变量jfirsti )。 Array indices are integer. 数组索引是整数。

The array / loop indexes should be ints, not doubles. 数组/循环索引应该是整数,而不是双精度数。

When attempting to access set[j] for example, it complains about treating j as an int. 例如,当试图访问set[j] ,它抱怨将j视为int。

Change the variable types as below. 更改变量类型如下。 Array indices must be of type 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