gpt4 book ai didi

java - 如何显示列表中的最小值和最大值?

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

我需要显示列表中的最小和最大当前价格,但我只能在末尾显示数字

public StockPrice() {
char startChar = 'A';
String tmpSymbol = null;
int startPrice = 0;
int priceRightNow = 0;
for (int idx = 0; idx < MAX_STOCKS; ++idx) {

tmpSymbol = "" + (char) (startChar + idx) + (char) (startChar + idx + 1) + (char) (startChar + idx + 2);

startPrice = ThreadLocalRandom.current().nextInt(minStockPrice, maxStockPrice + 1);

priceRightNow = ThreadLocalRandom.current().nextInt(minStockPrice, maxStockPrice + 1);

myStocks[idx] = new Stock(tmpSymbol, startPrice, priceRightNow);
System.out.println(myStocks[idx]);

System.out.println();

}
int[] val = {priceRightNow};
List<Integer> myStocks = new ArrayList<Integer>();
for (Integer number : val ) {

myStocks.add(number);
}

System.out.println("The current lowest stock price is " + tmpSymbol +Collections.min(myStocks));
System.out.println("The current highest stock price is "+ tmpSymbol +Collections.max(myStocks));
}

最佳答案

您正在覆盖您的变量.. collections.min, max 应该使您的最小值,最大值超出列表。

public class Test {

public static void main(String args[]) {

ArrayList<Integer> integerList = new ArrayList<>();
integerList.add(10);
integerList.add(1);
integerList.add(12);

System.out.println("MIN : "+Collections.min(integerList));
System.out.println("MAX : "+Collections.max(integerList));

}
}

此示例代码将为您提供以下输出。

MIN : 1
MAX : 12

关于java - 如何显示列表中的最小值和最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55942829/

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