I've been staring at this for two hours. Going back ad forth figuring out how to get one value to display. Once I do get one to show though, the other stops showing. My brain has died. I know this is a really easy fix too. -__-
import java.util.Scanner;
public class StockProfit
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
double todaysPrice = 0, price = 0;
double minPrice = 0, maxProfit = 0;
int today = 0, buyDay = 0, sellDay = 0, minDay = 0;
do{
while(todaysPrice >= 0){
today++;
System.out.print("Enter stock price for day " + today + " (any negative value to exit): $");
todaysPrice = s.nextDouble();
if (todaysPrice > minPrice) {
if (todaysPrice < minPrice){
minPrice = todaysPrice;
minDay = today;
buyDay = today;
}while(todaysPrice - minPrice > maxProfit) {
maxProfit = todaysPrice - minPrice;
sellDay = today;
}
System.out.println(minPrice);
System.out.println(minDay);
System.out.println(buyDay);
System.out.println(maxProfit);
System.out.println(sellDay);
}
}
if (maxProfit >= 0) {
System.out.println("Your max profit is $" + maxProfit + ", by buying on day " + buyDay + " and selling on day " + sellDay + ".");
} else {
System.out.println("No profit possible from that stock! Invest more wisely...");
}
}while (todaysPrice >= 0);
}
}
Can't get the minium price to calculate itself as well as the maxProfits and sellDays. I feel dumb.
import java.util.Scanner;
public class StockProfit
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
double todaysPrice = 0, price = 0;
double minPrice = 0, maxProfit = 0;
int today = 0, buyDay = 0, sellDay = 0, minDay = 0;
do{
while(todaysPrice >= 0){
today++;
System.out.print("Enter stock price for day " + today + " (any negative value to exit): $");
todaysPrice = s.nextDouble();
if (todaysPrice > minPrice) {
if (todaysPrice < minPrice){
minPrice = todaysPrice;
minDay = today;
buyDay = today;
}while(todaysPrice - minPrice > maxProfit) {
maxProfit = todaysPrice - minPrice;
sellDay = today;
}
System.out.println(minPrice);
System.out.println(minDay);
System.out.println(buyDay);
System.out.println(maxProfit);
System.out.println(sellDay);
}
}
if (maxProfit >= 0) {
System.out.println("Your max profit is $" + maxProfit + ", by buying on day " + buyDay + " and selling on day " + sellDay + ".");
} else {
System.out.println("No profit possible from that stock! Invest more wisely...");
}
}while (todaysPrice >= 0);
}
}
Can't get the minium price to calculate itself as well as the maxProfits and sellDays. I feel dumb.