#Having trouble with variable incrementation (+1)

1 messages ยท Page 1 of 1 (latest)

mild tundraBOT
#

<@&987246399047479336> please have a look, thanks.

mild tundraBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

pastel glen
#

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

#

๐Ÿ™‚

ashen kettle
#

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

acoustic forum
#

that's why it always shows 1

ashen kettle
#

oh so it resets every time the button is pressed?

#

wait no because then the random variable would change each time

acoustic forum
#

from this snippet i assume that is what is happening

ashen kettle
#

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

acoustic forum
#

congrats!

ashen kettle
#

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

tight epoch
#

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

late dock
#

or just totalGuesses++

sand plover
#

Or ++totalGuesses as it's 4.41 ns faster pepekek

late dock
#

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

high lance
#

but with latest cpus it barely makes any diff

pastel glen
#

actually, i benchmarked all flavors of increment in jmh

#

there is no measurable difference

high lance
#

coz ur cpu too good

#

use a 2 decades old cpu might get few ns diff then

coral terrace
pastel glen
#

with no measurable difference

#

i suppose we have had that discussion a year before already or so, and then I created it

coral terrace
#

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

sand plover
#

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

pastel glen
#

which is why many c devs have the habit of using ++i in loops over i++

sand plover
#

That makes sense

#

The point was that i++ requires the old value of i to be stored in a placeholder to use in an expression which is slightly slower

#

But modern compilers optimize it