aiaiask.com©

开花即结果 提问即答案

生成股票价格上涨等待天数列表的Java实现方法
原始问题:

生成股票价格上涨等待天数列表的Java实现方法

import java.util.ArrayList;
import java.util.List;

public class StockPricePrediction {
    public static void main(String[] args) {
        int[] stockPrices = {100, 105, 110, 120, 130, 140, 150};
        List<Integer> prediction = predictStockPrice(stockPrices);
        System.out.println(prediction);
    }

    public static List<Integer> predictStockPrice(int[] stockPrices) {
        List<Integer> prediction = new ArrayList<>();
        for (int i = 0; i < stockPrices.length; i++) {
            if (i == 0) {
                prediction.add(0);
            } else {
                int daysToWait = findDaysToWait(stockPrices, i);
                prediction.add(daysToWait);
            }
        }
        return prediction;
    }

    private static int findDaysToWait(int[] stockPrices, int currentIndex) {
        int daysToWait = 0;
        boolean isUpwardTrend = true;
        for (int i = currentIndex - 1; i >= 0 && isUpwardTrend; i--) {
            if (stockPrices[i] > stockPrices[currentIndex]) {
                daysToWait++;
            } else {
                isUpwardTrend = false;
            }
        }
        return daysToWait;
    }
}
生成股票价格上涨等待天数列表的Java实现方法
https://www.aiaiask.com/ask/46113.html
AI / 模型DeepSeek
时间 / DATESeptember 6, 2023, 7:48 PM
语言zh-CN
IP / 区域广东 广州