类型不匹配:无法从 double 转换为 Double

type mismatch: cannot convert from double to Double

 import java.util.Scanner;
 public class merge_sort 
{
public static void main(String[] args) 
{
    Scanner input= new Scanner (System.in);
    System.out.println("Hello, how many numbers there should be in the array?");
    int Size=input.nextInt();
    Double A []=new Double [Size] ;
    System.out.println("Please enter "+ (Size+1)+" real numbers");
    for (int z=0;z<Size;z++)
        A[z]=input.nextDouble();
    int p=0,q=(Size/2+1),r=(Size-1);//assuming that the array with even length.
    int L []=new int [4] ;//the left side, sorted array
    int R []=new int [4] ;//the right side, sorted array
    L[0]=7;L[1]=6;L[2]=2;L[3]=1;
    R[0]=5;R[1]=4;R[2]=3;R[3]=8;
    for(int i=0;i<4;i++)
        System.out.print(L[i]);
    System.out.println("");
    for(int j=0;j<4;j++)
        System.out.print(R[j]);

 merge(L,R);        
}

我在这行代码中有一个错误:

A[z]=input.nextDouble();

错误是:类型不匹配:无法从双精度转换为双精度

我卡住了几个小时,有人可以帮助我吗?

Doubleclass 类型。 nextDouble returns 原始类型 double。将 A 更改为 double 数组

double[] A = new double[Size];

比如盖伊的回答,或者你可以换行:

A[z]=input.nextDouble();

至:

A[z]=new Double(input.nextDouble());

有两种方法可以做到这一点。

  1. 通过使用 new 运算符调用 Double Class 的 constructor 并传递 input.nextDouble().

     A[z] = new Double(input.nextDouble());
    

2。 在 java 1.5 及之后引入的一项很酷的功能名为 autoboxing 所以,您也可以试试这个。

 A[z] = (Double)input.nextDouble();