-
Java (
http://www.daniweb.com/forums/forum9.html)
- -
Accumulators (
http://www.daniweb.com/forums/thread125789.html)
| NycNessyness | May 23rd, 2008 4:03 pm | |
| Accumulators Hello guys. I'm having a bit of difficulty with adding totals. I know how it's suppose to work but I'm not seeing what I'm doing wrong. If I want to add my total each time I hit the total button how can I do it. When I try to add it, it never works but lets say I do total = total +10;, it will do it only once no matter how many times I press the total button. This is an example of the code I have for now. So basically with this code, anytime I hit the total button it should keep adding 5 to the total. So the first time it will be 5, second time 10 and so forth. Thanks in advance!
total = 5;
newtotal = 0.0;
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if(arg == "Total")
{
total = total + newtotal;
}
} |
| Ezzaral | May 23rd, 2008 4:14 pm | |
| Re: Accumulators Um, you can 0 to a value all day and it won't change... total = 5;
newtotal = 0.0; |
| NycNessyness | May 23rd, 2008 4:25 pm | |
| Re: Accumulators I tried newtotal; instead of newtotal = 0; also and it didn't work. |
| Ezzaral | May 23rd, 2008 4:40 pm | |
| Re: Accumulators I would imagine that you are either resetting total or newtotal is not being captured like you think it is, because there is nothing wrong with that operation (except that you could use total += newtotal; instead). |
| NycNessyness | May 23rd, 2008 8:46 pm | |
| Re: Accumulators I found out what I had to do. It's lame when you find out you make the simplest of mistakes. I needed to add an extra line newtotal = total;. Then everytime i pick the total button it adds the old total to the new total. My new code with it implemented is below, incase others may have the same question in the future. Thanks again Ezz.
total = 5;
newtotal = 0.0;
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if(arg == "Total")
{
total = total + newtotal;
newtotal = total;
}
} |
| All times are GMT -4. The time now is 7:27 am. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC