我正在從我的教科書中的示例更改此代碼以便與雙數組兼容。有代碼的幾個地方,我不知道如何解決錯誤「不兼容的類型:雙重可能有損轉換成int」錯誤代碼不兼容的類型:可能有損從雙倍轉換爲int
temp = numberList[max];
numberList[max] = numberList[numberList.length - i - 1];
numberList[numberList.length - i - 1] = temp;
if (numberList[i] > numberList[index])
j = i;
temp = numberList[i];
那些顯示錯誤「不兼容的類型:從雙可能有損轉換INT「。
我也收到錯誤o第一次使用indexOfLargestElement,「找不到符號」。
package week5;
import java.util.Arrays;
/**
*
* @author meowens
*/
public class Week5 {
/**
* @param args the command line arguments
*/
public static void selectionSort(double [] numberList) {
double temp;
double max;
for (double i = 0; i < numberList.length - 1; i++)
{
max = indexOfLargestElement(numberList, numberList.length - i);
temp = numberList[max];
numberList[max] = numberList[numberList.length - i - 1];
numberList[numberList.length - i - 1] = temp;
}
}
private static double indexOfLarggestElement(double [] numberList, double size)
{
double index = 0;
for (double i = 1; i < size; i++)
{
if (numberList[i] > numberList[index])
index = i;
}
return index;
}
public static void insertionSort (double [] numberList)
{
int j, temp;
for (double i = 1; i < numberList.length; i++)
{
j = i;
temp = numberList[i];
while (j != 0 && numberList[j - 1] > temp)
{
numberList[j] = numberList[j - 1];
j--;
}
numberList[j] = temp;
}
// TODO code application logic here
// assigning values
numberList[0] = 53.5;
numberList[1] = 60.3;
numberList[2] = 96.2;
numberList[3] = 53.3;
numberList[4] = 56.4;
numberList[5] = 52.7;
numberList[6] = 76.4;
numberList[7] = 77.5;
numberList[8] = 71.0;
numberList[9] = 78.2;
numberList[10] = 65.2;
numberList[11] = 59.3;
numberList[12] = 80.5;
numberList[13] = 92.1;
numberList[14] = 85.7;
numberList[15] = 78.7;
numberList[16] = 66.2;
numberList[17] = 88.8;
numberList[18] = 50.2;
numberList[19] = 73.4;
}
}
我肯定還有其他簡單的錯誤,我還沒有找到,因爲我還沒有來得及調試和校對的代碼,我也很新的Java和編程一般。
感謝您的幫助!
「indexOfLargestElement()」上的「無法找到符號」表示該函數不存在。 「可能的有損轉換」意味着你在'int'變量中存儲了一個'double',它不包含完整的'double'值。 – David