【问题标题】:How to sum any amount of user inputted numbers from a single line如何从一行中汇总任意数量的用户输入数字
【发布时间】:2014-03-25 22:09:01
【问题描述】:

试图弄清楚我将如何从用户那里获取任意数量的输入数字并将它们加在一起

用户输入示例:1 2 3 4 总和 = 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);
  }

}

【问题讨论】:

  • 请显示您尝试解决问题的任何代码,以及您可能遇到的任何问题,然后您将获得一些指导。但是,我们不会为您完成(家庭)工作。
  • 查看 Java 中的 Scanner 类以获取标准输入。 docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
  • 不错的类名 :D :D
  • @rgettman 按要求添加了我的代码,也不是作业,而是做一些额外的事情,因为我对数字做的最后一件事是从 diff int 添加输入数据,但感谢您的回复。另外我遇到的问题是在第一个输入示例 123 123 将返回 123 而不是 246 之后没有读取任何数字。
  • @libik LOL 抱歉,我从 Lab 20 复制了大部分代码,这比 Lab 19 容易得多,我很生气,忘记更改了哈哈。最后也只是从我复制的东西中删除了所有代码并重写了它。

标签: java sum user-input


【解决方案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);
}

另外,如果您对“最小”版本感兴趣,它与之前的版本相同,但使用尽可能少的代码,这里是:

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

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

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

【讨论】:

  • 这很好用,但我将如何做到这一点,所以当我按下回车键时它会计算总和?我是否会将 score 更改为 String 并将其解析为 int 或其他内容,哈哈 另外查看一下我第一次尝试的代码与此非常相似,直到我开始更改内容以查看它是否可以工作。
  • 很好,感谢您的帮助,我从未见过 String line = kb.nextLine();和 kb = new Scanner(line);之前,必须对其进行一些研究,但与此同时,我将如何循环您提供的第一个代码?
  • @SamsinOzo - 在第一个代码中,您可以在一行中添加任意数量的数字,也可以添加任意数量的行。一旦你写“0”,它就会结束并打印出总和。例如尝试“1 2 3 4 5”回车,“10 20”回车,“5 5 5 0”回车
  • 我明白你的意思,嗯,我如何让它在输入之前打印每行的总和 Example 1 2 3 enter sum = 6, 1 2 3 4 输入 sum = 10
  • 感谢您的所有帮助我非常感谢您花时间帮助我,我添加了一个 if 语句来完成代码,这正是我所描绘的。再次感谢! 完成代码见主帖
【解决方案2】:

问题 - 继续将数字作为输入,直到用户输入“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();
            }
        }

【讨论】:

    猜你喜欢
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多