【问题标题】:How count the value of array? [closed]如何计算数组的值? [关闭]
【发布时间】: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


【解决方案1】:

好吧,试着猜测你想要什么,这就是我想到的:

import java.util.List;
import java.util.ArrayList;
import java.util.function.Predicate;

public class JavaExStockPrice {

    public static void main(String[] args){

        List<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("Avg: " + priceAvg(al));
        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("Avg: " + priceAvg(al));
        System.out.println();
    }

    static double priceAvg(List<Car> l) {
        return l.stream().mapToDouble(c -> c.buying * c.shares).average().getAsDouble() / 
                    l.stream().mapToDouble(c -> c.shares).average().getAsDouble();
    }

}



class Car {
    float shares;
    float buying;

    Car (float a, float b) {
        shares = a;
        buying = b;
    }

    void showSharesBuying() {
        System.out.println("showsharesbuying " + shares+ " : " + buying);
    }
}

对这样的静态方法进行逻辑处理不是一个好主意,但我不确定这是否是您想知道的。

【讨论】:

  • 我更详细,希望你理解,谢谢。
  • @sallytam 编辑了我的答案,认为每件商品的价格都是每股价格。这应该会给你你想要的。
  • 上面已经适配好了,但是如何通过另一个类来更新arraylist,比如添加备注:
  • 你能帮忙吗
  • 帮助什么?
猜你喜欢
  • 1970-01-01
  • 2020-01-31
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多