【发布时间】:2019-09-20 09:47:16
【问题描述】:
我需要在列表中显示当前最低和最高价格,但我只能在最后显示数字
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));
}
【问题讨论】:
-
您每次循环都在重新分配
tmpSymbol。循环完成后,它是最后一个股票的价值。
标签: java arrays list max minimum