• Post Reply Bookmark Topic Watch Topic
  • New Topic

Placing user input into For Statement using Scanner  RSS feed

 
Vincent Hammond
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I only started java so am a complete noob, forgive me in advance. Wondering if anyone can help.

Question:
Write a program that prompts the user to input two values- a start value and a finish
value. The first value is the initial / lower value in the range and the second value is
the finish/larger value in the range. Add up all even numbers between the initial value
and the finish value.

mycode so far:



Thats not the complete code obviously but were i'm stuck is, how do I get scanner to take the user input and use it as the initial state and the guard? once I know that I think i'm good to do the calculation.
Thanks
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code you should add "Code" tags to display your intended formatting. In the editor, highlight the code, then click on the "Code" button above the edit window.
I've fixed it for you this time.
 
Vincent Hammond
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks I will going forward
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need an int variable that you can use to keep track of how far along, between start and finish you are. Example:
Note that the above test (i<finish) is exclusive. In other words, up to but not including finish. If you want inclusive use i <= finish.
 
Vincent Hammond
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah i see, brilliant. thanks so much got that working now i'll just get the calculation done.
 
Denuwan Hettiarachchi
Greenhorn
Posts: 4
Android Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are interesting, check the following code segment. It will generate the same output like "for" iteration.



You can use the same variables for manage iteration, but make sure to add after all calculation inside the loop. Other wise It will miss some values.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are done, please post your final code back here.
 
Vincent Hammond
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I got this working have a few more programs done since as i have alot of work due for friday. I'm sure i'll be back very soon with more question :S Thanks so much guys!!!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't doing what you were asked to do: Add up all the even numbers between the initial and finish values.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I challenge you, after you have finished, to move any tests for odd and even numbers before the loop. Then you can write
for (i = something; something; i += 2) ...
and you only get even numbers inside the loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!