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
3,110
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
2
Location
This planet
XP
3,110
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
  • NinStar @ NinStar:
    there is a plugin that display them on the wii u menu, pretty sure it is enabled by default
  • crafthp434 @ crafthp434:
    so like it doesnt exist
  • crafthp434 @ crafthp434:
    yeah
    ?
  • NinStar @ NinStar:
    it doesn't exist, at least not for aroma
  • crafthp434 @ crafthp434:
    ohhhhh
  • NinStar @ NinStar:
    on tiramisu you can access it by opening mii maker
  • crafthp434 @ crafthp434:
    okay
  • NinStar @ NinStar:
    I don't have a wii u anymore to test it myself, but if homebrews are not visible on the wii u menu I think you can press L + R + minus to open the plugin menu, there should be an option called "homebrews on wii u menu" or something similar
  • crafthp434 @ crafthp434:
    nope
  • crafthp434 @ crafthp434:
    it is L+dpad down+ select
  • crafthp434 @ crafthp434:
    but homebrew is appearing in the home menu btw
  • NinStar @ NinStar:
    yes, now I remember it
  • NinStar @ NinStar:
    then it is working, I also don't like that they did this but it is the only option you have if you are using aroma
  • crafthp434 @ crafthp434:
    i just didint know the homebrew launcher didint exist in aroma
  • crafthp434 @ crafthp434:
    thanks btw
  • Xdqwerty @ Xdqwerty:
    Im downloading fallout 3 goty edition
    +1
  • BigOnYa @ BigOnYa:
    I'm downloading more ram for my hamster pc
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    New hamster PC, with anal operation and BT connectivity!
    +1
  • Xdqwerty @ Xdqwerty:
    @BigOnYa, How do I make enemies respawn on gdevelop after
    the player dies?
  • Psionic Roshambo @ Psionic Roshambo:
    Carrying a PC or phone is so old school!
  • Psionic Roshambo @ Psionic Roshambo:
    Squeeze your cheeks twice to answer calls!
  • BigOnYa @ BigOnYa:
    @Xdqwerty you can use a "spawner" function on any object.
    +1
  • BigOnYa @ BigOnYa:
    Or when your player dies, you can say in code, if enemy exists, do nothing, but if enemy does not exist, then create enemy at certain spot. (This would be a pain tho for lots of emeies)
    +1
  • BigOnYa @ BigOnYa:
    Easiest, simple way would be just restart scene, but player would restart from beginning.
  • Xdqwerty @ Xdqwerty:
    @BigOnYa, thx in advance
    +1
    Xdqwerty @ Xdqwerty: @BigOnYa, thx in advance +1