简体   繁体   中英

Printing the smallest element of an array and linking

Um, hi. So, I've been doing well with my beginner programing stuff until I came to one part concerning arrays and I am really stuck. I got a task to make two arrays, one with names and another one with money and I have to make a program which prints out the smallest amount of money (smallest array element) and the person who that money belongs to. I've tried to link them using a method I use when I sort from smallest to the biggest but here it doesn't work and it prints out the wrong thing.

package questions;
public class Questions {

    public static void main(String[] args) {

        String[]Names = {"Person","Human","Being"};
        int[]Money = {56,23,76};

            int minValue = Money[0];
            for (int i = 0; i < Money.length; i++) {

                if (Money[i] < minValue) {
                    minValue = Money[i];

                }

                System.out.println ("The person with least amount of money is: " + minValue + "  "+ Names[i]);


            }
    }
}

Can someone please tell me what do I have to do to make it only print out:

The person with least amount of money is: 23 Human

Just create a variable that will contain the min money index and print after the for loop:

    public static void main(String[] args) {

        String[]Names = {"Person","Human","Being"};
        int[]Money = {56,23,76};

        int minValue = Money[0];
        int minIndex = 0;
        for (int i = 0; i < Money.length; i++) {

            if (Money[i] < minValue) {
                minValue = Money[i];
                minIndex = i;
            }
        }
        System.out.println ("The person with least amount of money is: " + Money[minIndex] + "  "+ Names[minIndex]);
    }

You should write print line out of loop Like:-

public class Questions
{
    public static void main(String[] args)
    {
        String[]Names = {"Person","Human","Being"};
        int[]Money = {56,23,76};
        //Name having least money
        String namewithleatmoney = "";  
        int minValue = Money[0];
        for (int i = 0; i < Money.length; i++)
        {
            if (Money[i] < minValue)
            {
               minValue = Money[i];
               namewithleatmoney = Names[i];
           }
        }
        //Write this line out of loop
        System.out.println ("The person with least amount of money is: " + minValue + "  "+namewithleatmoney);
    }
}

Hope this will help

This is quite simple. you just need to track the index number on the smallest amount of the money from the array. See the code below you will understand. I have just saved the index of the smallest amount index in ** minValuIndex ** and in the end of the for loop I just printed the values depending on the index number.

package questions;
public class Questions {

    public static void main(String[] args) {

        String[]Names = {"Person","Human","Being"};
        int[]Money = {56,23,76};

            int minValue = Money[0];
            int minValuIndex = -1;

            for (int i = 0; i < Money.length; i++) {

                if (Money[i] < minValue) {
                    minValue = Money[i];
                    minValuIndex = i;

                }

                System.out.println ("The person with least amount of money is: " + 
                money[minValuIndex]+ "  "+ Names[minValuIndex ]);


            }
    }
}

try like this:

public static void main(String[] args) {
        String[] Names = { "Person", "Human", "Being" };
        int[] Money = { 56, 23, 76 };

        int minValue = Money[0];
        String personName = Names[0];
        for (int i = 1; i < Money.length; i++) {

            if (Money[i] < minValue) {
                minValue = Money[i];
                personName= Names[i];
            }
        }
        System.out.println("The person with least amount of money is: " + minValue + "  " + personName);
    }
package questions;
public class Questions {

    public static void main(String[] args) {

        String[]Names = {"Person","Human","Being"};
        int[]Money = {56,23,76};

            int minValue = Money[0];
            String minName = Name[0];
            for (int i = 0; i < Money.length; i++) {

                if (Money[i] < minValue) {
                    minValue = Money[i];
                    minName = Name[i];

                }

            }   
            System.out.println ("The person with least amount of money is: " + minValue + "  "+ minName[i]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM