开发者

Adding values in a for loop

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
 {
      publ开发者_如何学编程ic 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 . " );
     }
 }


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

    }
}


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

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


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


I believe you want sum = sum + run;

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


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

} 
}


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜