2015-12-03 117 views
-1

我被困在一個方法中,該方法將從週期表中的元素數組中找到最高沸點。我收到了錯誤,我無法從元素轉換爲雙倍。我嘗試投擲元素[0]加倍,但也沒有工作。任何幫助表示讚賞。Java在數組中找到最高值

public void findHighestBoilingPoint(Element[] elements, int size) 
    { 
     double highest = elements[0]; 

     for (int j = 1; j < elements.length; j++) 
     { 
      if (highest < elements[j]) 
      { 
       highest = elements[j]; 
      } 
     } 
     System.out.println(highest); 

    } 

這是我寫

public class Element 
{ 
    String elementName; 
    int atomicNumber; 
    String symbol; 
    double boilingPoint; 
    double meltingPoint; 
    double density; 
    double molecularWeight; 

    public Element(String elementName, int atomicNumber, String symbol, 
      double boilingPoint, double meltingPoint, double density, 
      double molecularWeight) 
    { 
     this.elementName = elementName; 
     this.atomicNumber = atomicNumber; 
     this.symbol = symbol; 
     this.boilingPoint = boilingPoint; 
     this.meltingPoint = meltingPoint; 
     this.density = density; 
     this.molecularWeight = molecularWeight; 
    } 

    public Element(String[] oneElement) 
    { 
     elementName = oneElement[0]; 
     atomicNumber = Integer.parseInt(oneElement[1]); 
     symbol = oneElement[2]; 
     if(!(oneElement[3].equals(""))) 
     { 
      boilingPoint = Double.parseDouble(oneElement[3]); 
     } 
     if(!(oneElement[4].equals(""))) 
     { 
      meltingPoint = Double.parseDouble(oneElement[4]); 
     } 
     if(!(oneElement[5].equals(""))) 
     { 
      density = Double.parseDouble(oneElement[5]); 
     } 
     molecularWeight = Double.parseDouble(oneElement[6]); 
    } 

    /** 
    * @return the elementName 
    */ 
    public String getElementName() 
    { 
     return elementName; 
    } 

    /** 
    * @return the atomicNumber 
    */ 
    public int getAtomicNumber() 
    { 
     return atomicNumber; 
    } 

    /** 
    * @return the symbol 
    */ 
    public String getSymbol() 
    { 
     return symbol; 
    } 

    /** 
    * @return the boilingPoint 
    */ 
    public double getBoilingPoint() 
    { 
     return boilingPoint; 
    } 

    /** 
    * @return the meltingPoint 
    */ 
    public double getMeltingPoint() 
    { 
     return meltingPoint; 
    } 

    /** 
    * @return the density 
    */ 
    public double getDensity() 
    { 
     return density; 
    } 

    /** 
    * @return the molecularWeight 
    */ 
    public double getMolecularWeight() 
    { 
     return molecularWeight; 
    } 

    @Override 
    public String toString() 
    { 
     String output = "Element Name:\t\t"+ elementName; 
     output = output + "\nAtomic Number: \t\t"+ atomicNumber; 
     output = output + "\nSymbol:\t\t\t + symbol"; 
     if(boilingPoint == 0) 
     { 
      output = output + "\nBoiling Point:\t\t unknown"; 
     } 
     else 
     { 
      output = output + "\nBoiling Point:\t\t" + boilingPoint; 
     } 
     if(meltingPoint == 0) 
     { 
      output = output + "\nMelting Point:\t\t unknown"; 
     } 
     else 
     { 
      output = output + "\nMelting Point:\t\t" + meltingPoint; 
     } 
     if(density == 0) 
     { 
      output = output + "\nDensity:\t\t unknown"; 
     } 
     else 
     { 
      output = output + "\nDensity:\t\t" + density; 
     } 
     if(molecularWeight == 0) 
     { 
      output = output + "\nMolecular Weight:\t\t unknown"; 
     } 
     else 
     { 
      output = output + "\nMolecular Weight:\t\t" + molecularWeight; 
     } 
     return output; 

    } 
+0

元素類看起來像什麼?您應該訪問課程的相關屬性進行比較。 –

+0

你有一個「元素」數組。是什麼讓你覺得你可以說'double = Element'?閱讀這些錯誤消息 - 他們在那裏幫助! – John3136

+0

你的方法簽名是這樣的嗎? :'public void findHighestBoilingPoint(double [] elements,int size)' – tanjir

回答

-1

你的陣列elements是由Element類型的值的數組元素類,但是當你聲明highest你讓一個double。由於highest類型爲double,因此存儲elements[0](一個Element)將不起作用。這就像試圖將圓釘插入方孔。

你需要看看你的元素類,並找出如何返回沸點 - 因爲這看起來像一個學校作業,可能有一個方法在那裏會拉出double出去,因爲你需要(或者你可以寫一個)。

+0

是的,我知道我不能以這種方式存儲它。我只是不確定從Element類中拉出什麼,以及如何將它應用到我寫的方法中。 –

+0

看着Element類,答案就在你的面前。仔細閱讀並思考你想要做什麼。 –