Stomped on this Loop

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.

Comments

[quote name='GamerzInc' post='2357336' date='Oct 29 2009, 05:15 AM']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();I guess here's the error. Your calculations should be alright. Maybe you're not getting a right vaue at this point so all your calculations fail.




if (todaysPrice > minPrice) {
if (todaysPrice < minPrice){this one will never turn out true because todaysprice needs to be bigger than minprice in the if caluse before this one, so minprice = 0 always
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.[/quote]

well I have no experience with java, but those are the things I noticed
 
What does it need to show exactly?
I pasted it in my compiler, maybe I'll find something

edit1: you never use price (it is set to 0 at the beginning)

edit2: another thing, since you minPrice is set to 0 at the beginning, it will ALWAYS be smaller than the todaysPrice.

ok, made some edits:
Code:
import java.util.Scanner; //Import scanner for button input

public class StockProfit {
	public static void main&#40;String&#91;&#93; args&#41; {				//main
		Scanner scanner = new Scanner&#40;System.in&#41;;
		double todaysPrice = 0;
		double minPrice = 0, maxProfit = 0;
		int today = 0, buyDay = 1, sellDay = 0, minDay = 0;
		do {
			while &#40;todaysPrice >= 0&#41; {
				today++;  //can also be set to ++today if you want
				System.out.print&#40;&#34;Enter stock price for day &#34; + today + &#34; &#40;any negative value to exit&#41;&#58; $&#34;&#41;;
				todaysPrice = scanner.nextDouble&#40;&#41;;

				if &#40;today == 1&#41; {			   //at the first day, minPrice has to be set to the todaysPrice
					minPrice = todaysPrice;
				}

				if &#40;todaysPrice > minPrice&#41; {
					if &#40;todaysPrice - minPrice > maxProfit&#41; {
						maxProfit = todaysPrice - minPrice;
						sellDay = today;
					}
				}

				if &#40;todaysPrice >= 0&#41; { //if price is below 0, skip all useless things

					if &#40;todaysPrice < minPrice&#41; {
						minPrice = todaysPrice;
						minDay = today;
						buyDay = today;
					}

					//Printing...
					System.out.println&#40;&#34;Minimun price&#58; &#34; + minPrice&#41;;
			 		System.out.println&#40;&#34;Day with min price&#58; &#34;+minDay&#41;;
					System.out.println&#40;&#34;Buyday&#58; &#34;+buyDay&#41;;
					System.out.println&#40;&#34;Max Profit&#58; &#34;+maxProfit&#41;;
					System.out.println&#40;&#34;Sell day&#58; &#34;+sellDay&#41;;
				}
			}

			if &#40;maxProfit > 0&#41; { //change >= into > because if its 0, there's no profit as well
				System.out.println&#40;&#34;Your max profit is $&#34; + maxProfit + &#34;, by buying on day &#34; + buyDay + &#34; and selling on day &#34; + sellDay + &#34;.&#34;&#41;;
			} else {
				System.out.println&#40;&#34;No profit possible from that stock! Invest more wisely...&#34;&#41;;
			}
		} while &#40;todaysPrice >= 0&#41;; //end of do while loop


	}
}
I added some comments
I think its supposed to be like this :)
Whats the difference between buyDay and minDay?
 
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){ I think that you have to delete this if statement (and put it somewhere else of course), as a previous /poster said the
minPrice = todaysPrice /satement will not execute due to minPrice Always being less than todaysPrice in the beginning. I don't see what would be to gain from using it there./*
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);


}
}

//I really don't see anything else wrong :)
//Coming from a guy with two highschool years of java under his belt :)

EDIT: Damn it, I can't get the text to show right :(
 
I only had Java for like 2 months :P

Anyway, I don't know if the op has advanced far enough to understand this, but this:
Code:
if &#40;maxProfit > 0&#41; { //change >= into > because if its 0, there's no profit as well
				System.out.println&#40;&#34;Your max profit is $&#34; + maxProfit + &#34;, by buying on day &#34; + buyDay + &#34; and selling on day &#34; + sellDay + &#34;.&#34;&#41;;
			} else {
				System.out.println&#40;&#34;No profit possible from that stock! Invest more wisely...&#34;&#41;;
			}

can be replaced with:
Code:
System.out.println&#40;maxProfit > 0 ? &#34;Your max profit is $&#34; + maxProfit + &#34;, by buying on day &#34; + buyDay + &#34; and selling on day &#34; + sellDay + &#34;.&#34;
					   &#58; &#34;No profit possible from that stock! Invest more wisely...&#34;&#41;;

Just as extra ;)
 

Blog entry information

Author
GamerzInc
Views
152
Comments
10
Last update

More entries in Personal Blogs

More entries from GamerzInc

Share this entry

General chit-chat
Help Users
    Xdqwerty @ Xdqwerty: @SylverReZ, ok then lets better switch to another topic... How are you?