在for循环中添加值

时间:2010-10-14 16:43:35

标签: java for-loop

我无法弄清楚如何在吐出数字后添加值。 它说:

  

号码:5 //我输了5个   1 2 3 4 5
  总和是。

所以我需要添加这些数字1 2 3 4 5,但无法弄清楚如何。

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

8 个答案:

答案 0 :(得分:2)

您需要将run添加到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=run+1)
        {
            System.out.print( run + " " );
            sum = sum + run; 
        }

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

    }
}

答案 1 :(得分:1)

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

+ sum似乎很奇怪,但你可以使用它和数字值到字符串

答案 2 :(得分: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 ); 
    }
}

答案 3 :(得分:0)

我相信你想要sum = sum + run;

如果你想要总和(1,2,3,4,5)= 15。

答案 4 :(得分:0)

你做这件事的方式你需要keyboard.nextLine();将所有数字设置为一个字符串。一旦你将所有数字都包含在字符串中,你就可以解析字符串并将其设置为扫描程序,然后执行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);
     }
 }

答案 5 :(得分:0)

法赫德的回答几乎就是你要找的东西(他在我打字的时候贴出来)。我的答案只是有一点不同的语法来执行循环和求和。

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

} 
}

答案 6 :(得分:0)

以下是可能有用的代码:

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

答案 7 :(得分:0)

希望这个答案可以帮到你。

假设输入值为6,所以内部添加所有连续数字直到6. I.e由于输入值为6,输出应该是这样的 0 + 1 + 2 + 3 + 4 + 5 = 15

请参阅以下代码段

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