我被困在一個方法中,該方法將從週期表中的元素數組中找到最高沸點。我收到了錯誤,我無法從元素轉換爲雙倍。我嘗試投擲元素[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;
}
元素類看起來像什麼?您應該訪問課程的相關屬性進行比較。 –
你有一個「元素」數組。是什麼讓你覺得你可以說'double = Element'?閱讀這些錯誤消息 - 他們在那裏幫助! – John3136
你的方法簽名是這樣的嗎? :'public void findHighestBoilingPoint(double [] elements,int size)' – tanjir