【问题标题】:Calling An Addition Method For An ArrayList of Class Objects为类对象的 ArrayList 调用加法方法
【发布时间】:2017-10-09 15:06:35
【问题描述】:

我目前正在开发一个程序,我必须收集数据并通过添加数字并根据以下数字修改它们来计算正确的输出。我为交易做的类是这样的:

public static class OrderTransactions {
    String no;
    String loc;
    String co;
    String val;

    public OrderTransactions(String n, String l, String c, String v) {
        no = n;
        loc = l;
        co = c;
        val = v;
    }

    public int TotalAmount(ArrayList<String> x) {
        HashMap<Character,Double> hm = new HashMap<Character,Double>();
        for(int i = 0; i < x.size(); i++) {
            String[] s = x.get(i).split("(?<=\\d)(?=[a-zA-Z])");
            hm.put(s[1].charAt(0), Double.parseDouble(s[0]));
        }

        double[] nums = new double[5];
        int i = 0;
        for(Map.Entry<Character,Double> pair : hm.entrySet()) {
            switch(pair.getKey()) {
                case 'M' : 
                    nums[i] = pair.getValue() * 1000000;
                    break;
                case 'K' : 
                    nums[i] = pair.getValue() * 1000;
                    break;
                case 'H' : 
                    nums[i] = pair.getValue() * 100;
                    break;
                default : 
                    System.out.println("Info did not have correct following letter");
                    break;
            }
            System.out.println(nums[i]);
            i++;
        }

        int tot = 0;
        for(int j = 0; j < nums.length; j++)
            tot += nums[j];

        return tot;
    }
}

当我像这样在 main 中调用 TotalAmount 方法时遇到问题:

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    OrderTransactions[] obj = new OrderTransactions[5];
    String info;

    for(int i = 0; i < obj.length; i++) {
        System.out.println("Enter input as: 'num;loc;co;val'");
        info = sc.nextLine();
        String[] parts = info.split(";");
        obj[i] = new OrderTransactions(parts[0], parts[1], parts[2], parts[3]);
    }

    ArrayList<String> costs = new ArrayList<String>();
    for(int i = 0; i < obj.length; i++)
        costs.add(obj[i].val);
    int tot = obj.TotalAmount(costs);

    System.out.println("Total Amount: " + tot);
}

我正在尝试计算我创建的类的数组的所有部分“val”的总和。问题是我不能使用“int tot”行上的对象数组来调用它。我试过为 obj[i] 调用它,但这并没有给我正确的计算。我将如何从我的类对象数组中调用 TotalAmount 方法

更多信息,我应该用以下字母来修改数字,例如“234K”和“1.3M”。运行代码时,输​​入将类似于“1001;Online;Target Corp;234K”、“1002;Store;Advantec Corp;1.3M”和“1003;Online;Marvel Brothers;2.1M”。

【问题讨论】:

  • 不相关:从始终使用好名字开始。我知道您在 OrderTransactions 中的字段应该是什么意思。除此之外,还不清楚您的代码到底应该做什么。所以请仔细阅读minimal reproducible example 并相应地更新您的问题。

标签: java arrays class object


【解决方案1】:

1) 请不要以大写开头的函数命名,例如public int TotalAmount(ArrayList&lt;String&gt; x) 将其命名为public int totalAmount(ArrayList&lt;String&gt; x)

2) 尝试将此方法public int totalAmount(ArrayList&lt;String&gt; x) 移动到主类并使其成为静态。

public class Main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    OrderTransactions[] obj = new OrderTransactions[5];
    String info;

    for(int i = 0; i < obj.length; i++) {
        System.out.println("Enter input as: 'num;loc;co;val'");
        info = sc.nextLine();
        String[] parts = info.split(";");
        obj[i] = new OrderTransactions(parts[0], parts[1], parts[2], parts[3]);
    }

    ArrayList<String> costs = new ArrayList<String>();
    for(int i = 0; i < obj.length; i++)
        costs.add(obj[i].val);
    int tot = totalAmount(costs);

    System.out.println("Total Amount: " + tot);
}
public static int totalAmount(ArrayList<String> x) {
    HashMap<Character,Double> hm = new HashMap<Character,Double>();
    for(int i = 0; i < x.size(); i++) {
        String[] s = x.get(i).split("(?<=\\d)(?=[a-zA-Z])");
        hm.put(s[1].charAt(0), Double.parseDouble(s[0]));
    }

    double[] nums = new double[5];
    int i = 0;
    for(Map.Entry<Character,Double> pair : hm.entrySet()) {
        switch(pair.getKey()) {
            case 'M' :
                nums[i] = pair.getValue() * 1000000;
                break;
            case 'K' :
                nums[i] = pair.getValue() * 1000;
                break;
            case 'H' :
                nums[i] = pair.getValue() * 100;
                break;
            default :
                System.out.println("Info did not have correct following letter");
                break;
        }
        System.out.println(nums[i]);
        i++;
    }

    int tot = 0;
    for(int j = 0; j < nums.length; j++)
        tot += nums[j];

    return tot;
}
public static class OrderTransactions {
    String no;
    String loc;
    String co;
    String val;

    public OrderTransactions(String n, String l, String c, String v) {
        no = n;
        loc = l;
        co = c;
        val = v;
    }


}
}

【讨论】:

  • 对不起,我正在使用这些约束和命名,因为这是我在业余时间做的公司实践任务。否则我可能会采取不同的方式。
  • 使用大写字母命名方法名称是一种非常常见的做法。不知道为什么有人会如此强调不这样做。
  • 这是因为 Java 编码约定。您可以检查 java 库以及方法和类的语法。 javarevisited.blogspot.com/2014/10/…
猜你喜欢
  • 1970-01-01
  • 2013-06-08
  • 2013-08-28
  • 2015-03-25
  • 2019-05-30
  • 1970-01-01
  • 2012-04-19
  • 2017-03-05
  • 1970-01-01
相关资源
最近更新 更多