本文介紹了從 double 到 int 的可能有損轉(zhuǎn)換的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
為什么我會(huì)收到 Possible lossy conversion from double to int
錯(cuò)誤,我該如何解決?
Why am I getting the Possible lossy conversion from double to int
error and how can I fix it?
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;
}
}
}
}
如您所見(jiàn),我已經(jīng)嘗試在聲明變量時(shí)將 int
替換為靠近頂部的 double
,但它似乎沒(méi)有完成這項(xiàng)工作.
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.
推薦答案
將所有用作數(shù)組索引的變量從 double 更改為 int(即變量 j
、first
, i
).數(shù)組索引是整數(shù).
Change all your variables used as array indices from double to int (i.e. the variables j
, first
, i
). Array indices are integer.
這篇關(guān)于從 double 到 int 的可能有損轉(zhuǎn)換的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!