public class StockProfit { // Determine best buy-sell period // Author: Max Kamin, June 24, 1997 public static void main (String[] args) { int minPrice, // lowest price so far priceToday, // last price input profit = 0; // best profit possible so far // Determine initial minimum price System.out.print("Enter value of stock : "); priceToday = Keyboard.readInt(); minPrice = priceToday; // what is the next day's price? System.out.print("Enter value of stock : "); priceToday = Keyboard.readInt(); while (!Keyboard.eof()) { // Does a sell today maximize profit? if (priceToday - minPrice > profit) profit = priceToday - minPrice; // Is today a new minimum price? if (priceToday < minPrice) minPrice = priceToday; // Next day's price. System.out.print("Enter value of stock : "); priceToday = Keyboard.readInt(); } System.out.println("Maximum profit = " + profit + " dollars."); } }