为了账号安全,请及时绑定邮箱和手机立即绑定

我如何获得多个股票利润?

我如何获得多个股票利润?

catspeake 2022-07-27 20:33:45
这是我找到我可以从股票中获得的最大利润的解决方案。int[] aktiePris = new int[]{10, 7, 5, 8, 11, 9}; 是一个数组,其中索引是股票市场开盘后的几分钟,值是股票的价格。所以例如 aktiePris[60] = 300 表示股票的价值是 300,股票市场开市后一小时。现在我的代码返回了我可以从买卖一只股票中获得的最大利润。我希望能够购买不止一只股票。我怎样才能找到所有可能的利润并打印出来?import java.text.SimpleDateFormat;import java.util.Calendar;public class ProfitCalculator {    static int minValue, maxValue, maxDiff;    static Calendar timeMin, timeMax;    static int indeksMinMinut, indeksMaxMinut;    public static void main(String args[]) {        int[] aktiePris = new int[]{10, 7, 5, 8, 11, 9};        int profit = findProfit(aktiePris);        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");        timeMin = findTime();        timeMin.add(timeMin.MINUTE, indeksMinMinut);        timeMax = findTime();        timeMax.add(timeMax.MINUTE, indeksMaxMinut);        System.out.println("Best time & price for buying is " + timeFormat.format(timeMin.getTime()) + " for " + minValue + " EUR." + "\n"                + "Best time & price for selling is " + timeFormat.format(timeMax.getTime()) + " for " + maxValue + " EUR." + "\n"                + "Profit: " + profit);    }    public static int findProfit(int[] inputArray) {        if (inputArray.length < 1)            return 0;        maxDiff = 0;        minValue = inputArray[0];        maxValue = minValue;        for (int i = 1; i < inputArray.length; i++) {            if (inputArray[i] > maxValue) {                maxValue = inputArray[i];                indeksMaxMinut = i;                int priceDiff = maxValue - minValue;                if (priceDiff > maxDiff) {                    maxDiff = priceDiff;                }            } else if (inputArray[i] < minValue) {                minValue = maxValue = inputArray[i];                indeksMinMinut = i;            }        }
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

制作一个共享分析器类,它封装了您想要的功能:


class ShareAnalyzer {


    private final int[] aktiePris;

    private int sellMinut = 0, buyMinut = 0, buyPrice,  sellPrice;


    ShareAnalyzer(int[] aktiePris) {


        this.aktiePris = aktiePris;

        findProfit();

    }


    private void findProfit() {


        if (aktiePris.length < 1) return ;

        int minValue = aktiePris[0];

        int maxValue = minValue;

        int indeksMaxMinut = 0, indeksMinMinut = 0;


        for (int i = 1; i < aktiePris.length; i++) {

            if (aktiePris[i] > maxValue) {

                maxValue = aktiePris[i];

                indeksMaxMinut = i;

                int priceDiff = maxValue - minValue;

                if (priceDiff > getProfit()) {

                    sellPrice = maxValue;

                    buyPrice  = minValue;

                    sellMinut = indeksMaxMinut;

                    buyMinut = indeksMinMinut;

                }

            } else if (aktiePris[i] < minValue) {

                minValue = maxValue = aktiePris[i];

                indeksMinMinut = i;

            }

        }

    }


    //time in minutes from opening

    int getSellTime() {return sellMinut;}


    int getSellPrice() {return sellPrice;}


    //time in minutes from opening

    int getBuyTime() {return buyMinut;  }


    int getBuyPrice() {return buyPrice;}


    int getProfit() {   return sellPrice - buyPrice;    }

}

您还可以添加一个打印共享分析器信息的方法:


    //this could be implemented as `toString` of ShareAnalyzer

    private static void printShareInfo(ShareAnalyzer shareAnalyzer){

        System.out.println("Best time & price for buying is " + shareAnalyzer.getBuyTime() + " minutes after opening for " + shareAnalyzer.getBuyPrice() + " EUR." + "\n"

                + "Best time & price for selling is " + shareAnalyzer.getSellTime() + " minutes after opening for " + shareAnalyzer.getSellPrice() + " EUR." + "\n"

                + "Profit: " + shareAnalyzer.getProfit());

    }

ShareAnalyzer为要分析的每个共享构建一个新实例。


public static void main(String args[]) {


    int[] aktiePris1 = new int[]{10, 7, 5, 8, 11, 9};

    ShareAnalyzer shareAnalyzer1 = new ShareAnalyzer(aktiePris1);

    printShareInfo(shareAnalyzer1);


    int[] aktiePris2 = new int[]{2, 12, 4, 7 , 11, 9, 1 , 8};

    ShareAnalyzer shareAnalyzer2 = new ShareAnalyzer(aktiePris2);

    printShareInfo(shareAnalyzer2); 

}

您可以使用此链接运行代码并对其进行测试


查看完整回答
反对 回复 2022-07-27
  • 1 回答
  • 0 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信