当输入的整数数量未知时,扫描并总结所有整数输入

时间:2017-05-29 05:31:56

标签: java input sum

爪哇:

我想总结一下每行的整数,但不知道用户会输入多少个整数。

输入:

3
10 20
1 2 3 4 5 6
10 -20 50 -90

输出:

30
21
-50

我写道:

import java.util.Scanner;

public class Sum2 {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int k = input.nextInt();
        int total = 0;
        for (int i = 0; i <= k;i++){
            while(input.hasNext()){
                   total += input.nextInt();               
            }
        System.out.println(total);
        }
    }
}

但没有输出。 (3表示用户将键入3行整数)

4 个答案:

答案 0 :(得分:0)

试试这个(编码很多,但适用于任意数量的输入):

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    int count = 0;
    String line;
    ArrayList<Integer> total = new ArrayList<>();
    boolean isNext = true;

    System.out.println("Input a value or 'e' to exit:");
    do{
        try{
            line = scan.nextLine();
            String num[] = line.split("\\s+");

            int sum = 0;
            for(String a : num){
                //if entered value equal to character 'e' print done and sum
                if(a.equals("e")){
                    a = a.replace("e", "0");
                    sum += Integer.parseInt(a);

                    isNext = false;
                    System.out.println("Done!");
                }else{
                    sum += Integer.parseInt(a);
                }
            }
            total.add(sum);
        }catch(NumberFormatException e){
            System.out.println("Wrong input!");
        }

        count++;
    }while(isNext);

    for(Integer i: total){
        System.out.println(i);
    }
}

输出:

Input a value or 'e' to exit:
10 20
1 2 3 4 5 6
10 -20 50 -90 e
Done!
30
21
-50

<强>更新

以上程序是我自己的程序,在您更新之前根据您的问题添加。在您的更新中,您添加了您尝试过的程序。您更新的问题是:

  

没有输出。 (3表示用户将键入3行整数)

当您运行程序时,它没有运行错误,您无法看到终端或IDE控制台是否要求输入。

你可以添加数字但是循环永远不会停止(无限),即使你添加了三行。

原因是while循环:

while(input.hasNext()){
    total += input.nextInt();             
}

一旦程序点击while循环,它就永远不会回到for循环。因为条件hasNext()检查下一个输入并且它总是有(返回true)。

我可以解释的另一种方法是,编译器从输入中查找下一个标记,并且仍然可以在循环内收集输入时收集输入,而这是真的,它一直这样做。

<强>解决方案:

如果您可以将特殊字符或单词(&#34;退出&#34;)的变量保存到结束循环,那就很好了。但作为代码的解决方案,您可以声明一个变量并在每次迭代中递增它,直到它等于您的第一个输入数字。

代码应如下所示:

Scanner input = new Scanner(System.in);
int k = input.nextInt();
int total = 0;
int count = 0;
while(count != k && input.hasNext()){
    count++;
    total += input.nextInt();
}
System.out.println(total);

这将只获得一行。要获得更多行,请尝试上面的程序。阅读本文以了解有关hasnext()的更多信息。

答案 1 :(得分:0)

您可以在变量中保存每一行,然后为此行创建扫描程序以读取该行的每个数字,直到输入换行符为止:

  public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int k = Integer.parseInt(input.nextLine());
        int total;
        for(int i = 0; i < k; i++){
           total = 0; 
           String line = input.nextLine();
           Scanner lineToken = new Scanner(line);
            while(lineToken.hasNextInt()){   
                  total = total + lineToken.nextInt();                     
            }
            System.out.println(total);
        }           
  }

<强>输出

30
21
-50

答案 2 :(得分:0)

pathMatch: 'full'

答案 3 :(得分:-4)

创建将生成行的循环语句以及下面的示例程序将对每行的所有输入求和。

System.out.print("Input how many number");
int n = reader.nextInt();
int a;

for(int x; x<=n;x++) {
    System.out.print("Enter a number");
    a = reader.nextInt();

    return a+=a;
}
相关问题