【发布时间】:2017-12-06 04:58:54
【问题描述】:
对不起,我需要详细说明一下。
我正在用Java写简单的array记录3次股票购买;每一辆新车都以新交易的形式呈现:
~~ 1 股以 100 美元/股的价格购买。
以 200 美元/股的价格购买 2 股。
以 300 美元/股的价格购买 3 股。
共有 6 股在手。 ~~
如何计算每股平均买入价?通过不断增加新车而不断运行。
package javaex;
import java.util.ArrayList;
import java.util.function.Predicate;
public class javaExstockprice
public static void main(String[] args){
ArrayList<Car> al= new ArrayList();
al.add(new Car(1,100));
al.add(new Car(2,200));
al.add(new Car(3,300));
System.out.println("showsharesbuyingmethod-alltransaction = shares : buying");
al.forEach(c->c.showsharesbuying() );
System.out.println();
al.removeIf(c->c.shares>1);
System.out.print("transcation amount 1 share / below");
System.out.println();
al.forEach(c->c.showsharesbuying() );
System.out.println();
}
class Car
float shares;
float buying;
Car (float a, float b) {
shares = a;
buying = b;
}
void showsharesbuying() {
System.out.println("showsharesbuying " + shares+ " : " + buying);
}
【问题讨论】:
-
真正的问题是什么?
How to calculate the average buying price per share ?或者当列表改变大小时如何调用计算 -
首先如何添加代码来计算这 3 笔交易的平均股价。
-
我仍然不确定问题是什么,但是如果您想计算超出单车范围的东西,我建议您将汽车添加到比简单列表更多的东西中,它将跟踪所有“交易”并计算您想要计算的任何内容。
-
@BrunoJCM 以下是更改,希望您能提供帮助。
-
为什么要排除只有一项的交易?
标签: java arrays loops arraylist auto-renewing