• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Loops  RSS feed

 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered. When the loop finishes, display the accumulated total.

That's the question I've been trying to solve for the past 3 days and I'm running out of time.  Does anyone know how I can write this? 
 
Chris Sadowski
Greenhorn
Posts: 14
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you come up with so far?
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Sadowski wrote:What have you come up with so far?



I don't think I'm doing this right...
 
Chris Sadowski
Greenhorn
Posts: 14
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, its a start But in the question you are asked to take in the the input 10 times, and every time add it together.

First step to changing your code would be to make sure you ask for the input 10 times. Right now you only ask for it once, and then loop over 10 times. With a minior change to your code you can ask for the input 10 times. This should get you started! After that you have to make sure to keep track of the sum like the question asked.



Hope this helps.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Sadowski wrote:Ok, its a start But in the question you are asked to take in the the input 10 times, and every time add it together.

First step to changing your code would be to make sure you ask for the input 10 times. Right now you only ask for it once, and then loop over 10 times. With a minior change to your code you can ask for the input 10 times. This should get you started! After that you have to make sure to keep track of the sum like the question asked.

[code]

public static void main (String [] args) throws IOException{

System.out.print("Enter 10 numbers: ");
int i;
String input;
Scanner keyboard = new Scanner (System.in);

for (i = 0; i < 10; i++) {
System.out.println("Enter next number:");
input = keyboard.nextLine(); //just like you did before, but now in a loop
System.out.println("You entered: "+input); //'input' contains your number

}

keyboard.close(); //dont forget to close
}
[\code]

Hope this helps.




Thanks!   now it runs 10 times,  But How do I make it add up all 10 numbers?  I don't know where they are stored or how to use the + feature to add them all up.
 
Chris Sadowski
Greenhorn
Posts: 14
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should point you to what you need for this exercise:

http://www.wikihow.com/Find-the-Sum-of-Two-Numbers-in-Java
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Sadowski wrote:This should point you to what you need for this exercise:

http://www.wikihow.com/Find-the-Sum-of-Two-Numbers-in-Java



Yeah I totally get that.  But in a loop how does it know what loop number its on?  without breaking it?

lets say I make a loop that runs twice.
and the first loop I enter 5
and the second loop I enter 5

what are those variables called?

maybe input + input?  or would that just make it enter 55?
 
Chris Sadowski
Greenhorn
Posts: 14
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that's a good question.

I think the biggest thing you're missing is that you defined the 'input' variable as String. This will treat the '5' in your example as just a character. You wanted to treat it as a number - because then you can properly add them. First step would be changing 'String input' to 'int input' as int (short for integer) represents a numerical value.  When you do this, however, the line in your code 'keyboard.nextLine()' will not work, as it will expect to be saved into a String. So you need to get a next int from your 'keyboard' , instead of the nextLine() which is a String. Please take a closer look at the link I sent, and its all there ;)
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Sadowski wrote:Ok, that's a good question.

I think the biggest thing you're missing is that you defined the 'input' variable as String. This will treat the '5' in your example as just a character. You wanted to treat it as a number - because then you can properly add them. First step would be changing 'String input' to 'int input' as int (short for integer) represents a numerical value.  When you do this, however, the line in your code 'keyboard.nextLine()' will not work, as it will expect to be saved into a String. So you need to get a next int from your 'keyboard' , instead of the nextLine() which is a String. Please take a closer look at the link I sent, and its all there ;)


YES YOU'RE RIGHT!  Thank you so much! 


It worked!   thanks again so much.  I understand it now!    Is there any way you could look at my other posts?  I have a few other questions xD
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Sadowski wrote:Ok, that's a good question.

I think the biggest thing you're missing is that you defined the 'input' variable as String. This will treat the '5' in your example as just a character. You wanted to treat it as a number - because then you can properly add them. First step would be changing 'String input' to 'int input' as int (short for integer) represents a numerical value.  When you do this, however, the line in your code 'keyboard.nextLine()' will not work, as it will expect to be saved into a String. So you need to get a next int from your 'keyboard' , instead of the nextLine() which is a String. Please take a closer look at the link I sent, and its all there ;)



Wait...  I did something wrong...  I'll see If I can figure it out.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Sadowski wrote:Ok, that's a good question.

I think the biggest thing you're missing is that you defined the 'input' variable as String. This will treat the '5' in your example as just a character. You wanted to treat it as a number - because then you can properly add them. First step would be changing 'String input' to 'int input' as int (short for integer) represents a numerical value.  When you do this, however, the line in your code 'keyboard.nextLine()' will not work, as it will expect to be saved into a String. So you need to get a next int from your 'keyboard' , instead of the nextLine() which is a String. Please take a closer look at the link I sent, and its all there ;)


Great... what did I do wrong now... lol
 
Chris Sadowski
Greenhorn
Posts: 14
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much closer!

But right now lets say in the very first loop, you enter '5', so you make your input have a 5 in it, then when you do

sum = input+input+input....

You're effectively doing:

sum = 5+5+5+5...

Which is not exactly what you want.

In a loop you only want to add the 'input' once. But you need something to keep track of what the sum is so far.

if you start with sum=0; before you start your loop, and then think about what would happen if instead of
sum = input+input+input

you tried:
sum = sum + input;

 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Sadowski wrote:Much closer!

But right now lets say in the very first loop, you enter '5', so you make your input have a 5 in it, then when you do

sum = input+input+input....

You're effectively doing:

sum = 5+5+5+5...

Which is not exactly what you want.

In a loop you only want to add the 'input' once. But you need something to keep track of what the sum is so far.

if you start with sum=0; before you start your loop, and then think about what would happen if instead of
sum = input+input+input

you tried:
sum = sum + input;








So like this?
 
Chris Sadowski
Greenhorn
Posts: 14
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(sum + input);  will only print whats curretly saved in 'sum'. In your save you only saved sum = 0; You need add to the sum every time what was in the sum before, plus the new input value.

all you're missing is sum = sum + input;

Then you dont need to print (sum + input), you can just print the sum.

 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Sadowski wrote:System.out.println(sum + input);  will only print whats curretly saved in 'sum'. In your save you only saved sum = 0; You need add to the sum every time what was in the sum before, plus the new input value.

all you're missing is sum = sum + input;

Then you dont need to print (sum + input), you can just print the sum.





AHHHH I SEE.  I see what you mean!  Now it will keep track of the numbers and keep building!   Thank you!
 
Chris Sadowski
Greenhorn
Posts: 14
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just realizing

sum = sum + input;

may seem confusing to you. In programming, what this means is that whatever is on the right side of the '=' will be saved to the variable on the left.

This means if you start with sum =0, then make input = 5 (from the command line), when you do
sum = sum + input;, you will save 0+5 (so, 5) to the variable sum. In the next loop, when your input will be lets say 3, when you do the same sum = sum + input, sum was already 5, so you will save 5+3 to the 'sum' (so, 8).

But don't listen to my explanation directly. There are tons and tons of resources that can make it much clearer online. I strongly recommend you check them out, it's gonna save you a lot of time with your assignments in the future Good luck!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This line should be split into 2 lines: doing more than one thing on a line just makes the code that bit more difficult to read and understand. It would be far better to write:
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris: excellent help on this thread - have a cow.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Just one thing: never close a Scanner pointing to System.in. You must close Scanners pointing to files, etc, but not System.in.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!