[英]Finding the sum of all elements of a sequence, ending with the number 0 using do-while loop in Java
I am a beginner.我是初学者。 I appreciate if someone solves my problem in simple way in Java.
如果有人在 Java 中以简单的方式解决了我的问题,我将不胜感激。
Problem: Find the sum of all elements of a sequence, ending with the number 0. The number 0 itself is not included into the sequence and serves as a sign of cessation.问题:求一个序列所有元素的和,以数字 0 结尾。数字 0 本身不包含在序列中,作为停止的标志。 Sample Input: 3 6 8 0 and Sample Output: 17
示例输入:3 6 8 0 和示例 Output:17
I am writing following program:我正在编写以下程序:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
do {
sum += scanner.nextInt();
} while (scanner.nextInt() != 0);
System.out.println(sum);
}
And What I get is as following:我得到的如下:
Input: 3 6 8 0输入:3 6 8 0
Output: Output:
11 <---it should come out as 17. Please figure out where the code is wrong. 11 <---结果应该是17。请找出代码错误的地方。
Please try below code请尝试以下代码
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
int checker = 0;
do {
checker = scanner.nextInt();
sum += checker;
} while (checker != 0);
System.out.println(sum);
}
}
In your code value coming from scanner.nextInt()
in while loop
is being missed to be added.在您的代码值中,来自
while loop
中的scanner.nextInt()
被遗漏添加。 That's causing a issue这导致了一个问题
The problem with your code你的代码有问题
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
do {
sum += scanner.nextInt(); // consuming an int from keyboard with nextInt()
} while (scanner.nextInt() != 0); // consuming another int from keyboard with nextInt()
System.out.println(sum);
for input 3 6 8 0
对于输入
3 6 8 0
sum += scanner.nextInt(); //this nextInt() consumes 3 now sum has 3
scanner.nextInt() != 0 // this nextInt() consumes 6 but does not add to sum now sum has 3 only the 6 is ignored
sum += scanner.nextInt(); // same here this consumes 8 and add to sum now sum has 11 which is the output
scanner.nextInt() != 0 // this nextInt() consumes 0 which fails the condition
In short you are ignoring every second input简而言之,你忽略了每一秒的输入
The fix修复
Scanner scanner = new Scanner(System.in);
int sum = 0;
int num = 0;
while((num = scanner.nextInt()) != 0) { // nextInt() is called only once and value is cashed in num
sum += num; // add this num to sum
}
System.out.println(sum); // prints 17 as expected
I am a beginner.我是初学者。 I appreciate if someone solves my problem in simple way in Java.
如果有人在 Java 中以简单的方式解决我的问题,我将不胜感激。
Problem: Find the sum of all elements of a sequence, ending with the number 0. The number 0 itself is not included into the sequence and serves as a sign of cessation.问题:求序列中所有元素的总和,以数字 0 结尾。数字 0 本身不包含在序列中,并用作停止的标志。 Sample Input: 3 6 8 0 and Sample Output: 17
样本输入:3 6 8 0 和样本 Output:17
I am writing following program:我正在编写以下程序:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
do {
sum += scanner.nextInt();
} while (scanner.nextInt() != 0);
System.out.println(sum);
}
And What I get is as following:我得到如下:
Input: 3 6 8 0输入:3 6 8 0
Output: Output:
11 <---it should come out as 17. Please figure out where the code is wrong. 11 <---它应该是17。请找出代码错误的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.