2

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 . " );
     }
 }
1
  • There's an error in your example - it should say sum = sum + <something> Commented Oct 14, 2010 at 16:46

8 Answers 8

2

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 );

    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

1
import java.util.Scanner;

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

                int sum = 0;
                for(int i=1 ; i<=10 ; i++){
                    Scanner s = new Scanner( System.in);
                    System.out.println("Enter number" + " " + i);
                    int b = s.nextInt();
                    sum = sum + b;
                }
                System.out.println("The result is :" + sum ); 
    }
}

Comments

0

I believe you want sum = sum + run;

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

Comments

0

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 + "." ); 

} 
}

Comments

0

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);
     }
 }

3 Comments

Your for loop is incorrect. What do you mean by run > number. Your code won't compile or run.
'number' was intended to be 0, fixed.
Maybe you should actually try compiling and running your code first. You'll probably get an exception from your scanner.
0

Here is the code that might be helpful:

import java.util.Scanner;

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

            int sum = 0;
            for(int i=1 ; i<=10 ; i++){
                Scanner s = new Scanner( System.in);
                System.out.println("Enter number" + " " + i);
                int b = s.nextInt();
                sum = sum + b;
            }
            System.out.println("The result is :" + sum ); 
    }
}

Comments

0

Hope this answer help you.

Assume that input value is 6, So internally add all the consecutive numbers until 6. I.e Since the input value is 6, output should be like this 0+1+2+3+4+5=15

Refer to the below snippet

public void testAdding() {
    int inputVal = 6;
    String input = "";
    int adder = 0;
    for(int i=0; i < inputVal; i++) {
        input = String.valueOf(i);
        if(inputVal == (i+1)) {
            System.out.print(input);
        } else {
            System.out.print(input+"+");
        }
        adder =+ i + adder;
    }
    System.out.print("="+adder);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.