Stomped on this Loop

GamerzInc

Well-Known Member
OP
Member
Joined
Jul 7, 2006
Messages
345
Trophies
0
Location
Memphis
XP
351
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
0
XP
98
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
2
Location
This planet
XP
2,966
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
0
Age
31
Location
Texas
XP
1,079
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
2
Location
This planet
XP
2,966
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
 

You may also like...

General chit-chat
Help Users
  • K3N1 @ K3N1:
    Drugs are bad
  • AncientBoi @ AncientBoi:
    I should drink water more
  • AncientBoi @ AncientBoi:
    Can't wait for my new/used Laptop to arrive tomorrow :D
    +1
  • x65943 @ x65943:
    don't spay your cat and just keep it in doors - easy peasy
    +1
  • K3N1 @ K3N1:
    Indoor cats can still get random infections at any time without fault of the owner spaying can help neutralize $1k+ surgeries
  • M4x1mumReZ @ M4x1mumReZ:
    @K3N1, Drugs are bad, mmmkay.
  • M4x1mumReZ @ M4x1mumReZ:
    @AncientBoi, I hope it doesn't have cheeto crumbs and white stains when you get it.
    +1
  • M4x1mumReZ @ M4x1mumReZ:
    @AncientBoi, If you shoot white powder, then consider a doctor to get that checked out lol.
    +1
  • K3N1 @ K3N1:
    Or they can get out at any time without you realizing it and you could come home to a dozen more cats who'd want that 😟
    +2
  • AncientBoi @ AncientBoi:
    It's apart of being an Ancient one there matey. Arrrrgh
    +1
  • AncientBoi @ AncientBoi:
    Drugs are bad. But what are they injecting the cats for the proceedure @K3N1
  • K3N1 @ K3N1:
    Meth
  • K3N1 @ K3N1:
    I never said my cat wasn't a drug addict
  • AncientBoi @ AncientBoi:
    sad. death toll in Turkey passes 11,000
  • K3N1 @ K3N1:
    But it's not even thanksgiving
  • Shape @ Shape:
    @M4x1mumReZ Was the Castlevania DOS game actually impossible to finish?
    +1
  • M4x1mumReZ @ M4x1mumReZ:
    @Shape, I never got to the boss, the gameplay is pure garbage.
  • M4x1mumReZ @ M4x1mumReZ:
    Controls are very finicky and its hard to kill enemies if they keep coming back.
  • AncientBoi @ AncientBoi:
    I wouldn't even dare to try that
  • M4x1mumReZ @ M4x1mumReZ:
    @K3N1, Oh noooooo
    +1
    M4x1mumReZ @ M4x1mumReZ: @K3N1, Oh noooooo +1