Stomped on this Loop

GamerzInc

Well-Known Member
OP
Member
Joined
Jul 7, 2006
Messages
345
Trophies
1
Location
Memphis
XP
362
Country
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.
 

Raki

Well-Known Member
Member
Joined
Jul 16, 2008
Messages
554
Trophies
1
XP
108
Country
Gambia, The
GamerzInc said:
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.

well I have no experience with java, but those are the things I noticed
 

tj_cool

Site dev
Supervisor
Joined
Jan 7, 2009
Messages
10,064
Trophies
3
Location
This planet
XP
3,141
Country
Belgium
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:
CODEimport java.util.Scanner; //Import scanner for button input

public class StockProfit {
ÂÂÂÂpublic static void main(String[] args) {ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ//main
ÂÂÂÂÂÂÂÂScanner scanner = new Scanner(System.in);
ÂÂÂÂÂÂÂÂdouble todaysPrice = 0;
ÂÂÂÂÂÂÂÂdouble minPrice = 0, maxProfit = 0;
ÂÂÂÂÂÂÂÂint today = 0, buyDay = 1, sellDay = 0, minDay = 0;
ÂÂÂÂÂÂÂÂdo {
ÂÂÂÂÂÂÂÂÂÂÂÂwhile (todaysPrice >= 0) {
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂtoday++;ÂÂ//can also be set to ++today if you want
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂSystem.out.print("Enter stock price for day " + today + " (any negative value to exit): $");
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂtodaysPrice = scanner.nextDouble();

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂif (today == 1) {ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ //at the first day, minPrice has to be set to the todaysPrice
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂminPrice = todaysPrice;
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ}

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂif (todaysPrice > minPrice) {
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂif (todaysPrice - minPrice > maxProfit) {
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂmaxProfit = todaysPrice - minPrice;
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂsellDay = today;
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ}
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ}

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂif (todaysPrice >= 0) { //if price is below 0, skip all useless things

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂif (todaysPrice < minPrice) {
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂminPrice = todaysPrice;
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂminDay = today;
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂbuyDay = today;
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ}

ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ//Printing...
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂSystem.out.println("Minimun price: " + minPrice);
ÂÂÂÂÂÂÂÂÂÂÂÂ ÂÂÂÂÂÂÂÂSystem.out.println("Day with min price: "+minDay);
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂSystem.out.println("Buyday: "+buyDay);
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂSystem.out.println("Max Profit: "+maxProfit);
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂSystem.out.println("Sell day: "+sellDay);
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ}
ÂÂÂÂÂÂÂÂÂÂÂÂ}

ÂÂÂÂÂÂÂÂÂÂÂÂif (maxProfit > 0) { //change >= into > because if its 0, there's no profit as well
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ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); //end of do while loop


ÂÂÂÂ}
}
I added some comments
I think its supposed to be like this
smile.gif

Whats the difference between buyDay and minDay?
 

Sterling

GBAtemp's Silver Hero
Member
Joined
Jan 22, 2009
Messages
4,023
Trophies
1
Age
33
Location
Texas
XP
1,110
Country
United States
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
smile.gif

//Coming from a guy with two highschool years of java under his belt
smile.gif


EDIT: Damn it, I can't get the text to show right
frown.gif
 

tj_cool

Site dev
Supervisor
Joined
Jan 7, 2009
Messages
10,064
Trophies
3
Location
This planet
XP
3,141
Country
Belgium
I only had Java for like 2 months
tongue.gif


Anyway, I don't know if the op has advanced far enough to understand this, but this:
Code:
if (maxProfit > 0) { //change >= into > because if its 0, there's no profit as well
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ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...");
ÂÂÂÂÂÂÂÂÂÂÂÂ}

can be replaced with:
Code:
System.out.println(maxProfit > 0 ? "Your max profit is $" + maxProfit + ", by buying on day " + buyDay + " and selling on day " + sellDay + "."
ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ : "No profit possible from that stock! Invest more wisely...");

Just as extra
wink.gif
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    U @ update-freak: Due to RSS did not work, is it possible to use Changedetection (Docker container) to recognise...