• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cant add twice

 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
i'm inside this method and want to add 2 to a var. Next, also inside the same method, i want to add to the same var lets say 2(again). I expected to get var = 5, but no!
I allways get ...var = 2
Looks like everytime i add, the adding starts from beguinning, rewriting my var, not adding to the previous value.
Well, i tried static and non static vars, sets and gets, i dont know what else to do
Can someone pls help me?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your code and we'll have a look
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for answering
ok here goes a bit of it:

I just cant spot where i'm doing it wrong.
Imagine 1rst and 3rd conditions meet - then hmm = 4, but no! Allways hmm =2!
Dont get it.
[ April 16, 2004: Message edited by: miguel lisboa ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "+=" operator adds the left operand to the right operand. But note the order of the two symbols: "+=". You've written
hmm =+ 2;
writing "=+" instead of "+=". The compiler is interpreting this as
hmm = (+2);
thus your variable is always set to 2.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your help, in first place
i'm using two separated checking methods, and both dont add twice, even with +=
take a look:
i use this method:

and also the hmm variable.
have a look, pls:

when two conditions meet, var hmm should be 4 or 5 and then the string should print either "Dois Pares" or "Full House!", but never never does...
i really puzzled
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't you be passing hmm to the contador() method instead of hardcoding 2 or 3 as you have here "contador(2)"? If I read this right if you have a pair, hmm = 2, if you have another pair hmm = 2 + 2, when you call contador() you'll be passing hmm which should equal 4 which gives you "Dois Pares" which I'm guessing means "Two Pairs".
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what i'm trying to do is to spot a "Two Pairs" or "Full House"" hand.So i'm adding 2 or 3 to that var each time i find two or three of the same kind, and is the var adds 5 i print Full House andif it values 4 i can for sure print Two Pairs. The problem it that my "adding method" doesnt work
I've two parallel ways of doing it: with an outside method contador() AND with internal var "hmm". but BOTH DONT add twice. Dont know what else to do
btw, thanks for answering!
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CASE SOLVED!!!
what was i doing wrong?
Using a return after every if, so the program you stop executing the method and hence wouldnt see the other number to add
Many thanks to all who helped!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic