2015-12-03 74 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类,答案就在你的面前。仔细阅读并思考你想要做什么。 –