• Post Reply Bookmark Topic Watch Topic
  • New Topic

Novice Question - Problem using while loop  RSS feed

 
Gilmar Chaves
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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?



 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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]
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4650
318
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gilmar Chaves wrote:So, i made some changes, but still, the same error.
What error?
 
Campbell Ritchie
Marshal
Posts: 55759
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 55759
163
  • Mark post as helpful
  • send pies
  • 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.
 
James Jenkins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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 !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!