Originally Posted by
Wwong3333
YAY!! i figured it out
That's a really big Yay! (Old-timers used to say, "Huzzah!")
Originally Posted by
Wwong3333
... make the user input 0 when he/she finishes...
Well, if you haven't figured out this part yet, try making the program tell you what it is seeing and what it is doing at each place where it makes a decision. Maybe something like:
[
Edit]
Note that in a premature posting of this snippet, I had inadvertantly pasted duplicate pieces of code. Sorry. The intent was (and is) to use the same code as in the Original Post but with a couple of debugging print statements added.[
/Edit]
//Prompts the user to insert price for assets
System.out.println("Please enter the price for asset" +count+ " or enter 0 when finished: ");
price = keyboard.nextDouble();
totalPrices = 0.0; // Set the accumulator to zero
mean = 0.0; // Set the mean to zero
minimum = 0.0; // Set the minimum to zero
maximum = 0.0; // Set the maximum to zero
//Get the assets prices and calculate a running total.
while(price != 0)
{
// Add price to totalPrices
totalPrices +=price;
// Get the price for the assets
System.out.println("Please enter the price for asset" +count+ " or enter 0 when finished: ");
price = keyboard.nextDouble();
count++;
//Calculates the minimum
if(price < minimum)
{
// For debugging:
// Let the programmer know what it found
System.out.println("New minimum = " + price);
minimum = price;
}
//Calculates the maximum
if(price > maximum)
{
// For debugging:
// Let the programmer know what it found
System.out.println("New maximum = " + price);
maximum = price;
}
// From Zaphod_b: For debugging, why not print out the values
// of total and count here so that you can see what it is using
// when it calculates the mean?
// Calculate the mean
mean = totalPrices / count;
}
Here's a run. Program output is
blue, user input (that's me) is
black.
Please enter the amount of assets you are tracking
and the price of each asset you are tracking
Enter 0 when finished.
Please enter the price for asset1 or enter 0 when finished:
100
Please enter the price for asset1 or enter 0 when finished:
50
New maximum = 50.0
Please enter the price for asset2 or enter 0 when finished:
25
Please enter the price for asset3 or enter 0 when finished:
0
The mean of your asset prices are 43.75
The mininum of your asset prices are 0.0
The maximum of your asset prices are 50.0
Hmmm... what happened? It didn't get the maximum correct and it didn't get the minimum correct. It also didn't get the average correct. How could that happen?
Since not a single output value is correct, should we throw it away and start all over again. Maybe that's the best thing in some programs that I have seen (and some that I have written), but not for this one. The sequence is correct, right? (Repeatedly asks for user input and terminates when the pre-defined sentinal value is encountered.) It gets answers; the only problem is that they are wrong. In other words, it's easier to fix
something that's wrong than it is to fix
nothing.
Here's the thing: If a program has substantially correct behavior (starts and stops OK) but gives wrong outputs for some input sequences, then those are the easiest kind to debug. I mean the output is staring you in the face, but it's wrong. (Much harder are programs that act erratically or programs that go into infinite loops with stack overflow messages until they crash and stuff like that.)
Bottom line: Your code is a pretty good start. Let's run with it.
Anyhow...
I'll get you started:
Well, you set value of maximum to zero after the user had already entered the first value, so that one didn't participate in the comparison looking for the maximum value. Fix that one first. (You don't need to reorganize the program in any way unless you really want to. Just be more smarter in how you determine the maximum of
all of the user input values.)
My suggestion is that you not worry about the other discrepancies just yet. Maybe something will occur to you as you debug the first problem. In the meanwhile, just concentrate on one problem at a time. That's the ticket!
If you make a change that gets the correct maximum value for the sequence of inputs in my example, try some more runs where values go up and down and back up and back down again. Make absolutely sure that the part that determines the max value is bullet-proof.
Now that you have inspected the innards of the program to the extent of being able to fix the first thing, maybe other changes have occurred to you. Try them, one at a time. (Just make sure that subsequent fixes don't unfix the first one.)
And so it goes...
Cheers!
Z