2016-03-15 95 views
0

我正在迭代一個int數組來查找中值,然後返回中位數,這部分程序似乎工作。如何將一個int數組上的操作的結果轉換爲double?

但是,中位數必須轉換爲double,它顯示的是小數位,但它沒有產生正確的輸出,爲什麼?

import java.util.*; 

public class Example 
{ 
    public static void main(String[] args) 
    { 
     int[] exampleArray = {2, 5, 10, 19, 23, 34}; 
     System.out.println("Median is: " + findMedian(exampleArray)); 
     // The output produced here should be 14.5 not 14.0 
    } 

    public static double findMedian(final int[] tempArray) 
    { 
     int median = 0, 
     Index = tempArray.length/2; 

     if(tempArray.length % 2 == 1) 
     { 
      median = tempArray[Index]; 

      /* I believe the problem is breaking down here I can't cast Index or 
       the tempArray to a double. I can copy the array elements into a new double array 
       but I tried that as well and the output was still off. 
      */ 
     }  
     else 
      median = (tempArray[Index] + tempArray[Index - 1])/2; 

     return (double)median; 
    } 
} 

回答

1

當你懷疑你的問題在於線

median = (tempArray[Index] + tempArray[Index - 1])/2; 

此行是做整數除法。整數除法是當你將一個整數除以另一個整數時發生的情況,並且它將結果分層,所以5/2是2,即使5.0/2.0是2.5。

你需要一個像

double preciseMedian = (tempArray[Index] + tempArray[Index - 1])/2.0; 

小數點使2.0雙線路,這使得整個師的發生是由於小數位。然後你需要將它存儲在一個雙精度的變量中,因爲你不能把它放在int中。

+0

哈,我甚至沒有考慮過,但它是有道理的謝謝你的迴應和解釋! – Afflicted

0

您的索引應該是int。

double median = 0; 
int Index = tempArray.length/2; //should be int 

if(tempArray.length % 2 == 1) 
{ 
    median = tempArray[Index]; 

    /* I believe the problem is breaking down here I can't cast Index or 
    the tempArray to a double. I can copy the array elements into a new double array 
    but I tried that as well and the output was still off. 
*/ 
}  
else 
    median = (tempArray[Index] + tempArray[Index - 1])/2.0; 
+0

太棒了!謝謝你:) – Afflicted

0

問題是你已經聲明中位數爲int並在int上執行計算並將其存回int。現在精度丟失了,然後它被轉換爲double,這不會重新獲得精度,因此請更改這些代碼部分。

double median = 0;  
    median = ((double)tempArray[Index] + (double)tempArray[Index - 1])/2; 
0

你可能會改變位數的數據類型將翻一番,確保你正在做一個雙重價值的一個部門,像這樣:

double median = 0; 

Index = tempArray.length/2; 

if(tempArray.length % 2 == 1) 
{ 
    median = tempArray[Index]; 
}  
else 
{ 
    /* You're having an integer division here. That means it's just 
    cutting off the digits. A later cast from int to double would do 
    nothing. There were no digits before. 
    */ 
    median = (double)(tempArray[Index] + tempArray[Index - 1])/2; 
} 
    return median; 
} 
0

你的問題是這行代碼:median = (tempArray[Index] + tempArray[Index - 1])/2;

你的median變量是一個int,你試圖給它分配一個double,所以java只是去除表達式的浮動部分,並給你一個int。

此行:return (double)median;將您的整數變量(這是浮點移除的中值變量)作爲double返回,所以它不返回double,而是返回浮點數(.0)的整數。

解決辦法:

變化int median = 0;double median = 0.0;。這將保持浮點。

0

嘗試通過聲明中位數爲雙倍。

double median = 0;

0

在Java 8+,您可以使用IntSummaryStatistics

System.out.println(IntStream.of(exampleArray).summaryStatistics().getAverage()); 

在早期版本的Java,我看最簡單的解決辦法是要總結的元素,然後通過元素的個數除以總數(其中是的中位數)like

public static double findMedian(final int[] tempArray) { 
    long total = 0; 
    for (int val : tempArray) { 
     total += val; 
    } 
    return (double) total/tempArray.length; 
}