Java 在 for 循环中添加值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3935498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:10:55  来源:igfitidea点击:

Adding values in a for loop

javafor-loop

提问by JSosa23

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.

Number: 5 // 我输入了 5
1 2 3 4 5
总和是。

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

所以我需要添加这些数字 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 . " );
     }
 }

采纳答案by dogbane

You need to add runto sumand then print it out, like this:

你需要添加runsum然后打印出来,像这样:

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

    }
}

回答by Tony Ennis

I believe you want sum = sum + run;

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

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

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

回答by Jacob Nelson

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

好吧,您需要这样做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);
     }
 }

回答by Brian Driscoll

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.

Fahd 的答案几乎就是你要找的(他在我打字时发布的)。我的答案只是有一点不同的语法来执行循环和求和。

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

} 
}

回答by JeffCharter

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

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

+ sum 看起来很奇怪,但您可以将它和一个数字值用于字符串

回答by user2427801

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

回答by user2427801

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

回答by Eswaran Venkatesan

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

假设输入值为6,那​​么在内部将所有连续的数字相加直到6。即由于输入值为6,输出应该是这样的 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);
}