如何将int数组上的操作结果转换为double?

时间:2016-03-15 19:51:12

标签: java arrays casting

我正在迭代一个int数组来查找中位数,然后返回中位数,这部分程序似乎有效。

但是,中位数必须转换为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; 
    }   
}

7 个答案:

答案 0 :(得分: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为双精度,这使整个除法发生在小数位。然后你需要将它存储在double变量中,因为你无法将它放在int中。

答案 1 :(得分: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; 

答案 2 :(得分:0)

问题是你已经将中位数声明为int并对int执行计算并将其存储回int。现在精度丢失了,然后转换为双精度,但没有恢复精度,所以改变代码的这些部分。

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

答案 3 :(得分:0)

您可能会将中位数的数据类型更改为double,并确保您正在进行双值的除法,如下所示:

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; 
}

答案 4 :(得分: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;。这将保持浮点。

答案 5 :(得分:0)

尝试将中位数声明为双倍。

double median = 0;

答案 6 :(得分:0)

在Java 8+中,您可以使用IntSummaryStatistics之类的

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

在早期版本的Java中,我看到的最简单的解决方案是对元素求和,然后将总数除以元素数(即中位数),如

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