简体   繁体   English

n 个整数扫描器的总和

[英]Sum of n-amount of Integers Scanner

I have to enter an unknown amount of numbers using a scanner.我必须使用扫描仪输入未知数量的数字。 Once the user enters -1, the program needs to print the sum of all the numbers entered and then end the program.一旦用户输入-1,程序需要打印所有输入数字的总和,然后结束程序。 The -1 needs to be included in the sum. -1 需要包含在总和中。

Scanner scanner = new Scanner(System.in);
int sum = 0;               

while (true) {
  int read = scanner.nextInt();
  if (read == -1) {
    break;
  }

 read = scanner.nextInt();
 sum += read;
}

System.out.println(sum);

I can't get the correct sum.我无法得到正确的金额。 Can someone help please?有人可以帮忙吗?

In your while loop you're assigning read two times using scanner.nexInt() and this breaks your logic.在您的 while 循环中,您使用scanner.nexInt()分配了两次read ,这破坏了您的逻辑。

Scanner scanner = new Scanner(System.in);
int sum = 0;               

while (true) {
    int read = scanner.nextInt();
    if (read == -1) {
        sum += read; //have to include -1 to sum
        break;
    }

    //read = scanner.nextInt(); you have to delete this line

    sum += read;
}

System.out.println(sum);
}
Scanner scanner = new Scanner(System.in);
int sum = 0;               
int read = 0;
while (read != -1) {
   read = scanner.nextInt();
   sum += read;
}
scanner.close();
System.out.println(sum);

This works in my case.这适用于我的情况。 Closing the scanner will remove the warning too.关闭扫描仪也将删除警告。

The following program will add -1 also in your sum.以下程序还将在您的总和中添加 -1。 You are reading read = scanner.nextInt();您正在阅读read = scanner.nextInt(); two times.两次。

 Scanner scanner = new Scanner(System.in);
        int sum = 0;               

        while (true) {
            int read = scanner.nextInt();


            sum += read;
            if (read == -1) {
                break;
            }
        }

        System.out.println(sum);
    }

This should work这应该工作

Scanner key = new Scanner(System.in);
int sum = 0;
int read;

while(read != -1) {
    read = key.nextInt();
    sum += read;
}

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

You can't just specify while(true/false) you have to give the loop a proper expression so it can work你不能只指定 while(true/false) 你必须给循环一个正确的表达式,这样它才能工作

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

相关问题 如何添加'n'个奇数? - How to add 'n' amount of odd integers? 总计为A的N个整数的不同组的数量 - number of different groups of N integers that sum up to A 有没有办法在Java中仅通过单个或n个字符使扫描仪前进? - Is there a way to advance the scanner only a by a single or n amount of characters in Java? 获取具有旧数字1 +(1 + 2)+(1 + 2 + 3)+…+(1 + 2 + 3 +…+ n)的整数和 - Get sum of integers with old numbers 1 + (1 + 2) + (1 + 2 + 3) + … + (1 + 2 + 3 + … + n) 给定整数集的子集,其和为常数 N:Java - Subsets of a given Set of Integers whose sum is a Constant N : Java 编写一个程序来计算前n个正整数的阶乘和。 - Write a program to calculate the sum of the factorials of the first n positive integers? 找出将 n 表示为两个有边界整数之和的方法的数量 - Find the number of ways to represent n as a sum of two integers with boundaries 使用递归从 N 个数字中提取 X 个整数的最大总和 - Max Sum of X integers out of N numbers using recursion 递归方法,将计算至少n个整数的数组中前n个整数的和。 以第n个整数开头 - Recursive method that will compute the sum of the first n integers in an array of at least n integers. Begin with nth integer 使用具有整数序列的扫描程序 - Using Scanner with sequence of Integers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM