运行该方法后,它只是循环遍历并打印整个数组和该索引中的内容。
想知道我做错了什么以及如何使它只打印出熔点最低的数组信息。
public void choiceFive() {
double smaller = elementArray[0].getMeltingPoint();
for(int i = 0; i < count; i++){
if(elementArray[i].getMeltingPoint() < smaller){
smaller = elementArray[i].getMeltingPoint();
}
System.out.println(smaller);
}
}
答案 0 :(得分:1)
我假设您有一个Element
类,其中包含名称,meltingPoint和其他一些属性,您希望找到elementArray
的最低熔点。
我实现了一个返回LowestMeltingPoint
元素的方法:
null
或其empty
返回 null 。currentLowestMeltingPoint
,否则请保留迭代。注意:它还会打印当前最低的信息用途。
public class Element
{
private String name;
private double meltingPoint;
public Element ( String name , double meltingPoint )
{
super ( );
this.name = name;
this.meltingPoint = meltingPoint;
}
public String getName ( )
{
return name;
}
public void setName ( String name )
{
this.name = name;
}
public double getMeltingPoint ( )
{
return meltingPoint;
}
public void setMeltingPoint ( double meltingPoint )
{
this.meltingPoint = meltingPoint;
}
@Override
public String toString ( )
{
return "Element [name=" + name + ", meltingPoint=" + meltingPoint + "]";
}
}
public class MainProgram
{
/**
* Main method to test getLowestMeltingPoint method
* @param args
*/
public static void main ( String [ ] args )
{
Element helium = new Element ( "Helium" , -272 );
Element radon = new Element ( "Radon" , -71 );
Element mercury = new Element ( "Mercury" , -39 );
Element sulfur = new Element ( "Sulfur" , 113 );
Element polonium = new Element ( "Polonium" , 254 );
Element[] elementArray = {mercury, sulfur, radon, polonium, helium};
Element lowestMeltingPointE = getLowestMeltingPoint ( elementArray );
System.out.println ( "----------------------------------------------" );
System.out.println ( "My lowest melting point is: " + lowestMeltingPointE.getName ( ));
System.out.println ( "Its melting point is: " + lowestMeltingPointE.getMeltingPoint ( ) );
System.out.println ( "element toString: " + lowestMeltingPointE.toString ( ) );
}
/**
* this method finds the lowest melting point element
* @param elementArray, which is the element array you want to compare
* @return element with lowest point or null if array is null or empty
*/
public static Element getLowestMeltingPoint( Element[] elementArray)
{
Element currentLowest;
if( elementArray == null || elementArray.length == 0)
{
return null;
}
else
{
currentLowest = elementArray[0];
}
for ( Element element : elementArray)
{
if ( element.getMeltingPoint ( ) < currentLowest.getMeltingPoint ( ) )
{
currentLowest = element;
}
System.out.println ( "Current lowest: " + currentLowest.toString ( ) );
}
return currentLowest;
}
}
注意:该方法是静态的。如果你不知道它是什么,请检查:Understanding Class Members
Current lowest: Element [name=Mercury, meltingPoint=-39.0]
Current lowest: Element [name=Mercury, meltingPoint=-39.0]
Current lowest: Element [name=Radon, meltingPoint=-71.0]
Current lowest: Element [name=Radon, meltingPoint=-71.0]
Current lowest: Element [name=Helium, meltingPoint=-272.0]
----------------------------------------------
My lowest melting point is: Helium
Its melting point is: -272.0
element toString: Element [name=Helium, meltingPoint=-272.0]
我只想给你完整的答案,因为那里有很多资源,当我开始使用Java时,我并不知道。
祝你好运!
答案 1 :(得分:0)
您必须将System.out.println(smaller);
移到for
循环之外。否则你继续打印它。
答案 2 :(得分:0)
将打印件移动到if块
if(elementArray[i].getMeltingPoint() < smaller){
smaller = elementArray[i].getMeltingPoint();
System.out.println(smaller);
}
或处理完成后
for(int i = 0; i < count; i++){
if(elementArray[i].getMeltingPoint() < smaller){
smaller = elementArray[i].getMeltingPoint();
}
}
System.out.println(smaller);
答案 3 :(得分:0)
您可以更改
for(int i = 0; i < count; i++)
到
for(int i = 0; i < elementArray.length; i++)
并将print语句移到循环外部,以便在所有循环中打印最低熔点。
for(int i = 0; i < elementArray.length; i++) {
if(elementArray[i].getMeltingPoint() < smaller){
smaller = elementArray[i].getMeltingPoint();
}
}
System.out.println(smaller);