• 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

Novice Question - Problem using while loop

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys, I was following a java tutorial trying to learn some code, but i got stuck trying somenthing, maybe, you guys can help me.

The code below should get a input (INT) from the user and make what we call here in brazil of 'Tabuada'. It should multiply the input * 1, 2, 3 ... and so on, until 10, then stop. My question is: Why my code is multiplying until 38?



 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gilmar Chaves wrote:My question is: Why my code is multiplying until 38?


Hi Gilmar, and welcome to JavaRanch!

What value did you type in for 'value', and what did you expect it to do?

Winston

[PS: Don't edit your posts while you're waiting for an answer. It confuses everybody]
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Gilmar.

Couple of things before I forget

Class name GettingUser_Input really should read as GettingUserInput, while both names are not as good as could be, but the second one fits to Java Coding Convention.
In Java there is no such data type as INT, there is an int. Java is a case sensitive language, that means, "Gilmar" is not the same as "gilmar", same as INT not the same as int.

Gilmar Chaves wrote:It should multiply the input * 1, 2, 3 ... and so on, until 10, then stop. My question is: Why my code is multiplying until 38?

Please tell us, what exact user inputs where, what the output you were expecting, and what actually you were getting? 38 currently doesn't say a lot (to me).
 
Gilmar Chaves
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Gilmar Chaves wrote:My question is: Why my code is multiplying until 38?


Hi Gilmar, and welcome to JavaRanch!

What value did you type in for 'value', and what did you expect it to do?

Winston

[PS: Don't edit your posts while you're waiting for an answer. It confuses everybody]




Hi Winston.

Sorry for the edit's.


So, I typed 2, so the variable 'value' should get this, right? Then, I expected it to show in the screen: 0, 2, 4, 6, 8 and so on, until 20 and then it shoud stop.


So, i made some changes, but still, the same error.

Here is the code



The output is in the attachment.
_output_.png
[Thumbnail for _output_.png]
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gilmar Chaves wrote:So, I typed 2, so the variable 'value' should get this, right? Then, I expected it to show in the screen: 0, 2, 4, 6, 8 and so on, until 20 and then it shoud stop.

Try to track down with the pencil and piece of paper.

So, you have:

value = 2
valueTi10 = 20
... keep going like that and you'll see if you agree with yourself or not.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gilmar Chaves wrote:So, i made some changes, but still, the same error.

What error?
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch again.

You have got a loop which is incrementing multiplicador. I suggest you start watching what the loop is doing by printing multiplicador inside the loop. You are confusing yourself by printing multiplicador * value;. If you still cannot understand why the loop is behaving as it is, go through the loop with a pencil and paper, like this:-…but do it correctly; I have introduced an error.
 
Gilmar Chaves
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Gilmar Chaves wrote:So, I typed 2, so the variable 'value' should get this, right? Then, I expected it to show in the screen: 0, 2, 4, 6, 8 and so on, until 20 and then it shoud stop.

Try to track down with the pencil and piece of paper.

So, you have:

value = 2
valueTi10 = 20
... keep going like that and you'll see if you agree with yourself or not.




Thanks Everyone, i got it now.

It was making 20 interactions, when it supposed to do only 11.





@Liutauras Vilda
Gonna read the Java Convention, thanks for pointing that out.



 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are incrementing multiplicador by 1 each time. Maybe you meant to use
multiplicador += value;
instead of multiplicador++;

And I see Liutauras got the answer a few minutes before I did.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry !   for some crazy reason i was reading it as  " i x 2 " .  
I need to stop skipping meals !

Thanks a lot guys !
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic