Results 1 to 7 of 7
Thread: Adding To An Integer
- 07-21-2011, 03:22 AM #1
Adding To An Integer
Feel free to laugh at me for this one, but I was building a rather complicated mod for a game and when I was trying to make so that when you hit a button it started a repeat loop. Unfortunately I couldn't figure out how to change the value of an int that was already set. I tried counter++ (because I used counter as a variable,) I tried to do counter.setValue, and many others. I have tried to look this up but I was unable to figure out how.
If what I was saying up there didn't make any since then basically I just want to add or subtract 1 from a variable.
Variable:
int counter = 1;
- 07-21-2011, 03:33 AM #2
Using counter++ will work. Why didn't work for you? We have no idea unless you post the relevant code.
Output is:Java Code:int counter = 1; System.out.println(counter); counter++; System.out.println(counter);
1
2
- 07-21-2011, 03:35 AM #3
Unless you fell for the ol post increment trick
Output is:Java Code:int counter = 1; System.out.println(counter); counter = counter++; System.out.println(counter);
1
1
If this what you have done then do a google search as this has been asked/discussed/explained ad infinitum.
- 07-21-2011, 03:40 AM #4
your right I wasn't thinking I had it like this.
unfortunately I don't know that I can change the other counter from different brackets can I?Java Code:int counter = 1; gwa.bu.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { counter++ } }); while(counter < 1){
- 07-21-2011, 03:42 AM #5
I actually think I have an idea I will make a separate class with that variable being public and just call the variable from the other class
- 07-21-2011, 03:52 AM #6
You probably got some error saying that the variable needed to be declared as final as you are trying to access it from the anonymous inner class. But you cannot make it final as you want to change its value. What you need to do is make the variable ans instance variable of the outer class and not local to the method.
- 07-21-2011, 03:59 AM #7
Similar Threads
-
Adding all elements of an Array together, not adding up correctly
By Teclis in forum New To JavaReplies: 1Last Post: 04-05-2011, 08:58 PM -
Adding integer to arraylist
By powerpravin in forum New To JavaReplies: 2Last Post: 04-03-2011, 07:21 AM -
Adding an integer to a double?
By tokoolio in forum New To JavaReplies: 5Last Post: 01-17-2011, 06:57 PM -
adding integer to the JPanel
By navid in forum New To JavaReplies: 2Last Post: 12-18-2010, 04:26 PM -
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks