【问题标题】:Need array assistance需要阵列协助
【发布时间】:2015-12-31 16:49:11
【问题描述】:

我正在上 Java 课程,但在开始作业时遇到了极大的困难。我不想要答案,但我真的很感激任何和所有的帮助,以及如何做的基本大纲。

一家超市想要奖励每天最好的顾客,在超市的屏幕上显示顾客的名字。为此,客户的购买金额存储在ArrayList<Double> 中,客户的姓名存储在相应的ArrayList<String> 中。

实现一个方法

public static String nameOfBestCustomer( ArrayList<Double> sales, ArrayList<String> customers)

返回销售额最高的客户的姓名。

编写一个程序,提示收银员输入所有价格和名称,将它们添加到两个数组列表中,调用您实现的方法,并显示结果。使用 0 的价格作为标记。

【问题讨论】:

  • 从你的问题的最后部分已经有了一个基本的大纲。虽然你说你不是在要求答案,但你是。如果您不知道数组列表是什么,请查看这些。
  • 那么,您从哪里开始,您的问题在哪里?我们愿意为您提供帮助,但您应该向我们展示您所做的一些前期努力。
  • 我投票结束这个问题作为题外话,因为它是一个家庭作业问题,但它不包括迄今为止为解决问题所做的工作的必要摘要,也没有描述解决它遇到的困难。

标签: java arrays methods arraylist


【解决方案1】:

您可以使用 Scanner 获取收银员输入的值(购买金额为 int,客户姓名为 String,使用条件和抛出异常来保证值的类型)。这些值放在两个 ArrayList(整数和字符串列表)中。 超市关门后,您可以在购买金额列表上使用循环来查找最大购买金额及其在列表中的位置,并使用该位置在另一个列表中找到最佳客户。

【讨论】:

    【解决方案2】:

    这是你可以做的:

    package com.assignments;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class MaxSalesCustomer {
    
       public static void main(String[] args) {
        // TODO Auto-generated method stub
          ArrayList<String> customerNameArray = new ArrayList<String>();
          ArrayList<Integer> customerSalesValue = new ArrayList<Integer>();
          String continueAdding = "Y"; 
          Scanner sc=new Scanner(System.in);
    
          while(continueAdding.equals("Y")){
             System.out.println("Please Enter the customer Name:");
             String name = sc.next();
             customerNameArray.add(name);
             System.out.println("Please Enter the sales value:");
             Integer sales = sc.nextInt();
             customerSalesValue.add(sales); 
             System.out.println("Do you want to continue 'Y/N' ?" );
             continueAdding = sc.next();
         }
    
         String maxSalesCustomerName = getMaxSalesCustomerName(customerNameArray,customerSalesValue);
         System.out.println(maxSalesCustomerName);
      }
    
      public static String getMaxSalesCustomerName(ArrayList<String> customerNameArray,ArrayList<Integer> customerSalesValue){
        Integer maxValue = 0;
        String maxSalesCustomerName = new String();
        for (int i = 0; i < customerSalesValue.size(); i++){
            if (maxValue < customerSalesValue.get(i)){
                maxValue = customerSalesValue.get(i);
                maxSalesCustomerName = customerNameArray.get(i);
            }
        }
         return maxSalesCustomerName;
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多