如何从一行汇总任何数量的用户输入数字

时间:2014-03-25 22:09:01

标签: java sum user-input

试图找出如何从用户那里获取任何数量的输入数字并将它们加在一起

示例用户输入:1 2 3 4 Sum = 10

用户可以将任意数量的数字放入指定的数量中,因此如果他想添加1 2 3 4 5 6 7 8 9 10 11 12 13,它们将总计为91

感谢您的帮助。

import java.util.Scanner;

public class test
{
    public static final int SENTINEL = -1;
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int score = 0;
        int sum = 0;

        System.out.println("Enter numbers here");
        while (score >= 0) {
            if (score <= -1) {
            score = kb.nextInt();
            sum += score;
            score = 0;
        }
            System.out.println(sum);
    }
  }
}

感谢libik所有的时间和帮助,这里是完成的代码。

import java.util.Scanner;

public class JavaApplication1156 {

    public static void main(String[] args) {
    System.out.println("Enter numbers here");
    int sum;
    do {
        Scanner kb = new Scanner(System.in);
        int score = 0;
        sum = 0;
        String line = kb.nextLine();
        kb = new Scanner(line); //has to do this to make the kb.hasNexInt() work.
        while (kb.hasNextInt()) {
            score = kb.nextInt();
            sum += score;
        }
        if (sum <= -1)
        System.out.println("Application ended");
        else if (sum >= 0)
        System.out.println("Sum = " + sum);

    } while (sum != -1);
  }

}

1 个答案:

答案 0 :(得分:1)

实际上很容易

import java.util.Scanner;

public class JavaApplication115 {

    public static void main(String[] args) {
        System.out.println("write numbers, if you write zero, program ends");
        Scanner input = new Scanner(System.in); //just copy-and paste this line, you dont have to understand it yet.
        int number;
        int sum = 0;
        do {
            number = input.nextInt(); //this reads number from input and store its value in variable number
            sum+= number; //here you add number to the total sum
        } while(number != 0); //just repeat cycle as long as number is not zero

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

}

根据您的代码运作代码:

public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    int score = 0;
    int sum = 0;

    System.out.println("Enter numbers here");
    String line = kb.nextLine();

    kb = new Scanner(line); //has to do this to make the kb.hasNexInt() work.
    while (kb.hasNextInt()) {
        score = kb.nextInt();
        sum += score;
    }
    System.out.println(sum);
}

此外,如果您对&#34; minimal&#34;感兴趣版本,与之前版本相同,但使用尽可能少的代码,这里是:

public static void main(String[] args) {
    int sum = 0;
    System.out.println("Enter numbers here");
    Scanner kb = new Scanner((new Scanner(System.in)).nextLine()); //has to do this to make the kb.hasNexInt() work.
    while (kb.hasNextInt()) {
        sum += kb.nextInt();
    }
    System.out.println(sum);
}

只要sum不为零(基于第二个代码块),找到每一行的总和:

public static void main(String[] args) {
    System.out.println("Enter numbers here");
    int sum;
    do {
        Scanner kb = new Scanner(System.in);
        int score = 0;
        sum = 0;
        String line = kb.nextLine();
        kb = new Scanner(line); //has to do this to make the kb.hasNexInt() work.
        while (kb.hasNextInt()) {
            score = kb.nextInt();
            sum += score;
        }
        System.out.println("Sum = " + sum);
    } while (sum != 0);
}