Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For-loop help initialising?

 
Tom-Colin Kelly
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have some problems because just for some practice in getting better in Java I am practicing a for-loop. I am aiming to create a program that allows the user to type 10 numbers into the computer and calculates the total. however i have made the loop 10 times, and the userInput, however i am not sure how to sum this, i am currently trying to make a variable which will add all of the user inputs into.

Here is my code:



I am just looking for some help why the 'num + userInput;' doesnt work and how i can fix this, thanks.

Tom
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom-Colin Kelly wrote:
I have some problems because just for some practice in getting better in Java I am practicing a for-loop. I am aiming to create a program that allows the user to type 10 numbers into the computer and calculates the total. however i have made the loop 10 times, and the userInput, however i am not sure how to sum this, i am currently trying to make a variable which will add all of the user inputs into.

Here is my code:



I am just looking for some help why the 'num + userInput;' doesnt work and how i can fix this, thanks.

Tom



Well, this is not a for-loop issue -- as that line won't have any effect regardless of having a for-loop.

Question? What do you think that line is supposed to do?

Henry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are REALLY close. Thanks for showing what you did so far.


This does add the two numbers. Then it throws away the result. You need an = on the line to save it.

num += userInput or num = num + userInput will save it to the num variable.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What your code is doing in the state you posted it is this: each time you enter a loop, a new value (the one entered by user) is assigned to userInput at line 11. At line 12 you are basically doing nothing: you add userInput and num variable (which is not initialized in your code so it has the value of zero), and the result of that sum is not assigned to any variable so it's useless.

What you can do to have the result you want is the following:
  • You declare a variable, say sum before for loop, and initialize it to zero. This variable will be used to accumulate inputs gathered by user and calculate total sum of them.
  • Inside the for loop, read the variable userInput as you did at line 11; that's the current input from the user.
  • After that add the value entered by user to the variable sum (not the way you did it at line 12).


  • Once you get out of the loop, you can print the total sum of the numbers entered.

    Also, there probably isn't a very good reason why you make those variables static. userInput can be declared as variable local to the method you posted (which it is, indeed), and that num doesn't really have any significance so you can as well remove it.
     
    Tom-Colin Kelly
    Greenhorn
    Posts: 27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Henry Wong wrote:


    Well, this is not a for-loop issue -- as that line won't have any effect regardless of having a for-loop.

    Question? What do you think that line is supposed to do?

    Henry


    i know it isnt the loop, its an issue with the naming of 'num', but i am not sure how to fix tis?
    i thin this is supposed to as for the input 10 times and each time it adds what number they added into the variable 'num' so if my first number is 3 and second is 3 the num after that will be 6?

    Thanks for the input
     
    Tom-Colin Kelly
    Greenhorn
    Posts: 27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeanne Boyarsky wrote:You are REALLY close. Thanks for showing what you did so far.


    This does add the two numbers. Then it throws away the result. You need an = on the line to save it.

    num += userInput or num = num + userInput will save it to the num variable.


    Thank you it worked perfectly
    Ive done simething simmilar before but i froget i needed the = too!
    And thanks for explaining it!
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50225
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please go back to your for loop and work out how often it will run. Be very careful.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic