gpt4 book ai didi

java - 为类对象的 ArrayList 调用加法方法

转载 作者:行者123 更新时间:2023-12-02 02:34:10 24 4
gpt4 key购买 nike

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

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”。

最佳答案

1) 请不要以应用程序大小写开头的函数命名,如 public int TotalAmount(ArrayList<String> x)将其命名为 public int totalAmount(ArrayList<String> x)

2)尝试移动这个方法public int totalAmount(ArrayList<String> 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 - 为类对象的 ArrayList 调用加法方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46649578/

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