簡體   English   中英

類型不匹配:無法從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();

錯誤是: 類型不匹配:無法從double轉換為Double

我被困了幾個小時,有人可以幫我嗎?

Double是一個class類型。 nextDouble返回原始類型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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM