簡體   English   中英

打印數組的最小元素並鏈接

[英]Printing the smallest element of an array and linking

嗯,你好。 所以,在我談到關於 arrays 的部分之前,我一直在做我的初學者編程工作,我真的被卡住了。 我的任務是制作兩個 arrays,一個有名字,另一個有錢,我必須編寫一個程序,打印出最小的錢(最小的數組元素)和錢所屬的人。 我嘗試使用從最小到最大排序時使用的方法將它們鏈接起來,但在這里它不起作用並且打印出錯誤的東西。

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


            }
    }
}

有人可以告訴我我該怎么做才能讓它只打印出來:

The person with least amount of money is: 23 Human

只需創建一個包含 min money 索引並在 for 循環后打印的變量:

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

您應該在循環之外寫打印行,例如:-

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

希望這會有所幫助

這很簡單。 您只需要跟蹤數組中最小金額的索引號。 看下面的代碼你就明白了。 我剛剛在 ** minValuIndex ** 中保存了最小數量索引的索引,並且在 for 循環結束時,我只是根據索引號打印了值。

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


            }
    }
}

試試這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM