gpt4 book ai didi

java - 在数组列表中搜索最大的数字

转载 作者:行者123 更新时间:2023-12-02 06:11:23 26 4
gpt4 key购买 nike

所以我现在正在编写一个代码来搜索数组列表并打印出最大的一个。此代码提示用户输入 BankAccount 名称以及该帐户有多少钱。目前我遇到了不知道如何在数组列表中搜索最大数字的问题。我也不知道如何将每个余额与 maxBalance 进行比较。感谢任何帮助,谢谢。

        import java.io.*;
import java.util.*;
import java.text.*;

public class Project43Tester
{
public static void main(String args[])
{
NumberFormat formatter = NumberFormat.getNumberInstance( );
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String name;
ArrayList <String> aryLst = new ArrayList<String>();

do
{
Scanner kbReader = new Scanner(System.in);
System.out.print("Please enter the name to whom the account belongs.(\"Exit\" to abort)");
name = kbReader.nextLine( );
if( !name.equalsIgnoreCase("EXIT") )
{
System.out.print("Please enter the amount of the deposit. ");
double amount = kbReader.nextDouble();
System.out.println(" ");
ArrayList <String> BankAccount = new <String> ArrayList();
AryLst.add(BankAccount);
}
}while(!name.equalsIgnoreCase("EXIT"));

Search aryList and print out the name and amount of the largest bank account

BankAccount ba = //get first account in the list
double maxBalance = ba.balance;
String maxName = ba.name;

for(int j = 1; j < aryLst.size( ); j++)
{
?
? Step through the remaining objects and decide which one has
largest balance (compare each balance to maxBalance)
?
}

System.out.println(" ");
System.out.println("The account with the largest balance belongs to " + maxName + ".");
System.out.println("The amount that the account contains is $" + formatter.format(maxBalance) + ".");
}
}

最佳答案

您将必须遍历 ArrayList。在遍历ArrayList的过程中,将第一个元素设置为max,并与next进行比较。如果更大,则将新元素设置为最大值。

关于java - 在数组列表中搜索最大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21847606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com