为什么我收到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;
}
}
}
}
正如您所看到的,我已经尝试在声明变量时尝试将int
替换为顶部附近的double
,但它似乎没有完成任务。
答案 0 :(得分:3)
将用作数组索引的所有变量从double更改为int(即变量j
,first
,i
)。数组索引是整数。
答案 1 :(得分:1)
数组/循环索引应该是整数,而不是双精度。
例如,当试图访问set[j]
时,它抱怨将j视为int。
答案 2 :(得分:1)
更改变量类型,如下所示。数组索引的类型必须为 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;
}
}
}
}