Re: Need urgent help with Java Project
Each time round that loop you are creating a new 'runningtot' variable.
At the end of the loop that variable goes out of scope.
If you want to keep it (which you do so you can add to it) then it needs to be declared outside of the loop.
Re: Variable to initialized
Re: Variable to initialized
Quote:
Originally Posted by
Tolls
See your other thread.
I'm merging the two threads. Bungie, please go through the Forum Rules -- particularly the second paragraph.
db
Re: Need urgent help with Java Project
Quote:
Originally Posted by
Tolls
Each time round that loop you are creating a new 'runningtot' variable.
At the end of the loop that variable goes out of scope.
If you want to keep it (which you do so you can add to it) then it needs to be declared outside of the loop.
I took out double runningtot; from the for loop. But now the program is outputting the user's most current input instead of totaling the price's.
Re: Variable to initialized
Can you show your output?
1 Attachment(s)
Re: Variable to initialized
Quote:
Originally Posted by
Tolls
Can you show your output?
With a printscreen? Attached.Attachment 4260
Where ti says Total Price: it should add up all the previous prices.
Re: Variable to initialized
OK, so what does your code look like now?
Specifically the bit around the loop (including the declaration of the runningtot variable).
Re: Variable to initialized
Quote:
Originally Posted by
Tolls
OK, so what does your code look like now?
Specifically the bit around the loop (including the declaration of the runningtot variable).
Code:
double runningtot;
Scanner input = new Scanner (System.in);
double[]runningArray = new double[14];
.
.
.
.
.
for(int count=0; count < runningArray.length; count++)
{
System.out.println("Please enter price of item: ");
double price = input.nextDouble();
System.out.println("");
System.out.println("That was $" + price);
System.out.println("");
runningtot =+ price;
System.out.println("Your total price is $" + runningtot);
}
Re: Variable to initialized
That should be '+=' not '=+'.