gpt4 book ai didi

java - 查找数组列表中的最大值

转载 作者:行者123 更新时间:2023-12-02 06:00:21 25 4
gpt4 key购买 nike

在我编写的这个程序中,我必须打印出在商店消费最多的顾客的姓名。我需要帮助在数组列表中搜索花费最多的客户。

 package bestcustomer;
import java.util.*;
/**
*
* @author muf15
*/
public class BestCustomer {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Double> sales = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
double salesAmount;
System.out.println("Enter the sales for first customer: ");
salesAmount = in.nextDouble();
while(salesAmount !=0)
{
sales.add(salesAmount);
System.out.println("Enter customers name");
names.add(in.next());
System.out.println("Enter the next sales amount, 0 to exit: ");
salesAmount = in.nextDouble();
}
String bestCustomer = nameOfBestCustomer(sales, names);

}
public static String nameOfBestCustomer(ArrayList<Double> sales,
ArrayList<String> customers)
{
String name = "";
double maxSales;



return name;
}


}

最佳答案

您应该将这两个字段包装在一个名为 Customer 的类中,然后

使用Collections.max();

Collections.max(yourCollection, customComparator);

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

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