比较并循环遍历数组以查找哪个元素具有较低的熔点

时间:2016-12-01 01:22:21

标签: java arrays

运行该方法后,它只是循环遍历并打印整个数组和该索引中的内容。

想知道我做错了什么以及如何使它只打印出熔点最低的数组信息。

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);
        }


    }

4 个答案:

答案 0 :(得分:1)

我假设您有一个Element类,其中包含名称,meltingPoint和其他一些属性,您希望找到elementArray的最低熔点。

获得最低熔点:

我实现了一个返回LowestMeltingPoint元素的方法:

  1. 如果elementArray为null或其empty返回 null
  2. 将第一个元素设置为currentLowestMeltingPoint(因为到目前为止)。
  3. 使用forEach(更干净)循环遍历数组并检查元素熔点是否低于当前值。如果 true :将元素设置为currentLowestMeltingPoint,否则请保留迭代。
  4. 注意:它还会打印当前最低的信息用途。

    元素类:

    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);