简体   繁体   English

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

[英]How to sum any amount of user inputted numbers from a single line

Trying to figure out how I would take any amount of inputted numbers from a user and add them together试图弄清楚我如何从用户那里获取任意数量的输入数字并将它们加在一起

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

The user can put any amount of numbers in not a specified amount so if he wanted to add 1 2 3 4 5 6 7 8 9 10 11 12 13, it would sum them all up to 91用户可以将任意数量的数字放入非指定数量中,因此如果他想添加 1 2 3 4 5 6 7 8 9 10 11 12 13,则它们的总和为 91

Thanks for the help in advance.我在这里先向您的帮助表示感谢。

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

Thanks to libik for all his time and help, here is the finished code.感谢 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);
  }

}

It is very easy actually 其实很容易

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

}

Working code based on your code : 根据您的代码工作的代码:

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

Also if you are interested in "minimal" version, which is the same as the one before, but using as less code as possible, here it is : 另外,如果您对“最小”版本感兴趣,该版本与之前的版本相同,但是使用的代码越少越好,这里是:

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

Find sum of each line as long as sum is not zero (based on second block of code) : 只要总和不为零,就可以找到每一行的总和(基于第二个代码块):

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

Question - Keep taking numbers as inputs till the user enters 'x', after that print sum of all.问题 - 继续将数字作为输入,直到用户输入“x”,然后打印所有内容的总和。

Scanner sc = new Scanner(System.in);
        int input = 0;
        int sum = 0;
        while (true){
            sum = sum+input;
            if (input==5){
                System.out.println("Loop is stopped");
                System.out.println("The sum is " + sum);
                break;
            }
            else {
                System.out.println("Take the inputs");
                input = sc.nextInt();
            }
        }

//Input the number in a single line (but be in size limit of integer) //在一行中输入数字(但在整数的大小限制内)

import java.util.Scanner;

public class Sum_of_integers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int sum = 0;
        while(num >= 1) {  //base value
            int lastval = num % 10;  //the logic
            num = num  / 10;
            sum += lastval;
        }
        System.out.println(sum);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在输入的二维数组java中找到奇数的数量和所有奇数的总和? - How to find the amount of odd numbers and thee sum of all odd numbers in a inputted 2D array java? JAVA:如何在循环中计算用户输入的数字的总和(数字存储在同一变量中) - JAVA: How to calculate sum of user-inputted numbers in a loop (numbers are stored in same variable) while 循环。 用户输入数字的总和和乘积 - while loop. sum and product of user inputted numbers 如何显示输入数字的 2 位数总和(质数) - How possible to display the 2 digit sum of the inputted number (prime numbers) 从用户那里读取一系列数字,直到输入-1,Java - Reading a series of numbers from the user until -1 is inputted, Java Java中平均用户输入的数字 - Average user inputted numbers in Java GUI Java-如何使用2个用户输入的数字获得答案 - GUI Java-how to use 2 user inputted numbers to get an answer 如何在Java中根据用户输入的整数打印数字? - How to print numbers based on user inputted integer in java? 如何将用户输入的整数插入数组? - How to insert inputted integers from user into an array? 如何排除所有有 1 的数字并打印用户输入的 rest 并将输入的数字与打印的数字匹配? - How to exclude every number that has 1 and print the rest that the user has inputted and match the inputted number with printed numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM