Chris Sadowski wrote:What have you come up with so far?
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.
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
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 ;)
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 ;)
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 ;)
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;
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.