#Having trouble with variable incrementation (+1)
1 messages ยท Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
i suspect u recreate the variable each time per guess
unfortunately i can only guess, since u didnt share all the code
but essentially u have to create ur variable beforehand
so that it stays alive
if u share the full code, we can give u a concrete suggestion
by the way, dont abbreviate variable names tot is bad. write it out
totalGuesses, better
๐
ooh ok Ill try what you said out, thanks so much. Ill send screenshots of the full code I currently have in a sec
Alright so the first image is the gui that shows up (this along with the guessing its self runs and works completely fine).
The second image is the full code that makes all the guessing work. In my mind, increasing totalGuesses+1 each time the guess button is pressed should work yet it still seems to just set the total to 1 indefinitely; even when higher and lower guesses are made.
(also thanks so much for taking the time to help me out, I appreciate it so much ๐ )
if nothing here works I think second best choice would maybe to create a while loop? Although I'd have to think about how I'd do that more.
Im pretty sure I can get it working with if/else statements though
I just moved from python so Im still working out the kinks of everything
i think it's because while you press the button you will call 'guessBtnActionPerformed' in which you will declare totalGuesses int. As you can notice - it is always set to 0 while calling the method
that's why it always shows 1
oh so it resets every time the button is pressed?
wait no because then the random variable would change each time
from this snippet i assume that is what is happening
I suppose that could be happening but I think if that was the case the guessing wouldn't work either
I can see if I can declare the variable outside the button action
IT WORKS
MY ACTUAL SAVIOUR
TYSM
congrats!
this is what I ended up with! I just declared it outside and then put an extra line at the bottom of the if statement
totalGuesses = totalGuesses+1;
Omg thank you
alr ill close this off now
If you want
You can put totalGuesses = totalGuesses + 1 before totalGuess.setText(...) so you don't have to do the unnecessary addition
Also you can use totalGuesses += 1 instead of totalGuesses = totalGuesses + 1 because it feels cleaner
or just totalGuesses++
Or ++totalGuesses as it's 4.41 ns faster 
surely isn't with nowadays compilers
i'd assume that both n = n + 1 and n++ is optimized to ++n
tested just in case, the bytecode between identical n++ and ++n is identical
n = n + 1 however produces different bytecode
no
but with latest cpus it barely makes any diff
actually, i benchmarked all flavors of increment in jmh
there is no measurable difference
@pastel glen @high lance
All i++, i++, i += 1 compiles to
iinc 1, 1
i = i + 1 compiles to
iload_1
iconst_1
iadd
istore_1
(Assuming local variable 1 is i, and all 4 expressions are a statement, so the return value isn't used)
and i have jmh benchmark for around 10 different kind of increments
with no measurable difference
i suppose we have had that discussion a year before already or so, and then I created it
yes there is no difference in time of time
but I just wanted to point out that specifically i++, i++, i += 1 are literally the same code
I just saw some benchmarking a while back, maybe it was for c idk that said ++i was faster.
My original message was just a satirical joke in case yall didn't get it
that has been the case for old c compilers, yes. (its not the case anymore though)
which is why many c devs have the habit of using ++i in loops over i++