tags:

views:

110

answers:

5

I cant figure out how to add the values after it spits out the numbers. it says:

Number: 5 // I typed 5
1 2 3 4 5
The sum is.

So i need to add those number 1 2 3 4 5 but cant figure out how.

 import java.util.Scanner
 public class AddingValuesWithAForLoop
 {
      public static void main( String[] args )
      {
          Scanner keyboard = new Scanner(System.in);
          System.out.println( " \n" );

          System.out.println( "Number: " );
          int number = keyboard.nextInt();
          int sum = 0;

          for (int run=1; run<=number; run=run+1)
          {
              System.out.print( run + " " );
              sum = sum + 1 ;
          }

          System.out.println( "The sum is . " );
     }
 }
A: 

You need to add run to sum and then print it out, like this:

import java.util.Scanner

public class AddingValuesWithAForLoop
{
    public static void main( String[] args )
    {

        Scanner keyboard = new Scanner(System.in);
        System.out.println( " \n" );

        System.out.println( "Number: " );
        int number = keyboard.nextInt();
        int sum = 0;

        for (int run=1; run<=number; run=run+1)
        {
            System.out.print( run + " " );
            sum = sum + run; 
        }

        System.out.println( "The sum is " + sum );

    }
}
dogbane
A: 

I believe you want sum = sum + run;

if you are wanting to sum (1, 2, 3, 4, 5) = 15.

Tony Ennis
A: 

well the way your doing it you will need to do keyboard.nextLine(); that will set all of your numbers to a string. Once you have all of your numbers in string you can parse through the string and set that as the scanner then do nextInt()

import java.util.Scanner
 public class AddingValuesWithAForLoop
 {
      public static void main( String[] args )
      {
          Scanner keyboard = new Scanner(System.in);
          System.out.println("Number: ");
          string numbers = keyboard.nextLine(); // 5 1 2 3 5
          Scanner theNumber = new Scanner(numbers);

          int sum = 0;

          for (int run = theNumber.nextInt(); run > 0; run--)
          {
              System.out.print(run + " ");
              sum += theNumber.nextInt();
          }

          System.out.println("The sum is: " + sum);
     }
 }
Jacob Nelson
Your `for` loop is incorrect. What do you mean by `run > number`. Your code won't compile or run.
dogbane
'number' was intended to be 0, fixed.
Jacob Nelson
Maybe you should actually try compiling and running your code first. You'll probably get an exception from your scanner.
dogbane
A: 

Fahd's answer is pretty much what you're looking for (he posted while I was typing mine). My answer just has a little bit different syntax to perform the loop and sum.

import java.util.Scanner

public class AddingValuesWithAForLoop { public static void main( String[] args ) {

    Scanner keyboard = new Scanner(System.in); 
    System.out.println( " \n" ); 

    System.out.println( "Number: " ); 
    int number = keyboard.nextInt(); 
    int sum = 0; 

    for (int run=1; run<=number; run++) 
    { 
        System.out.print( run + " " ); 
        sum += run; 
    } 

    System.out.println( "The sum is " + sum + "." ); 

} 
}
Brian Driscoll
+1  A: 
System.out.println( "The sum is: " + sum );

the + sum seems weird but you can use it the and a number value to a string

JeffC