[英]I can't get my java program to print result
I'm stuck on a program from mooc.fi course;我被 mooc.fi 课程困住了; wherein I can't get my program to print results.
其中我无法让我的程序打印结果。 The program should 'print all the numbers divisible by three in the given range.
该程序应该“打印给定范围内所有可被 3 整除的数字”。 The numbers are to be printed in order from the smallest to the greatest.'
数字按从小到大的顺序打印。
Thanks for the help.谢谢您的帮助。
public class DivisibleByThree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = Integer.valueOf(scanner.nextLine());
int b = Integer.valueOf(scanner.nextLine());
divisibleByThreeInRange(a, b);
}
public static void divisibleByThreeInRange(int beginning, int end) {
for (int i = 0; i >= beginning && i <= end; i++) {
if (i % 3 == 0) {
System.out.println(i);
}
}
}
}
Welcome to stack overflow, Entropy!欢迎来到堆栈溢出,熵!
The problem is this line:问题是这一行:
for (int i = 0; i >= beginning && i <= end; i++) { ... }
Let's break up that for
loop:让我们分解一下
for
循环:
for
says its a loop, with the first statement initializing the loop, the second giving the condition for executing the next iteration, and the third saying how to update after an interation. for
表示它是一个循环,第一个语句初始化循环,第二个语句给出执行下一次迭代的条件,第三个语句表示如何在迭代后更新。 Inside the quotes is what to execute in an interation.int i = 0
defines the loop variable i
and sets it to 0.int i = 0
定义循环变量i
并将其设置为 0。i >= beginning && i <= end
.i >= beginning && i <= end
。 So we will execute the next iteration if i
lies in the entered range.i
在输入的范围内,我们将执行下一次迭代。i++
just increments the counter.i++
只是增加计数器。 So effectively, you start with i
being 0, and then execute the loop WHILE that number is within the entered range.如此有效,您从
i
为 0 开始,然后在该数字在输入范围内时执行循环。 But if i
BEGINS outside your range, the loop is never executed , because the condition is false at the very beginning.但是,如果
i
超出您的范围,则永远不会执行循环,因为一开始条件为假。
You can confirm that by entering making the range contain 0, ie enter a non-positive lower and a non-negative upper range (like -10 to 10).您可以通过输入使范围包含 0 来确认这一点,即输入一个非正的下限和一个非负的上限范围(如 -10 到 10)。 Then, the initial condition is fulfilled and your loop happily shows all the number divisable by 3.
然后,满足初始条件,您的循环愉快地显示了所有可被 3 整除的数字。
So simply change the loop to所以只需将循环更改为
for (int i = beginning; i <= end; i++) { ... }
and it will work as intended: Start at the beginning of the range, and go the end of it -- done!它将按预期工作:从范围的开头开始,然后结束——完成!
These for-loops can be tricky sometimes, don't they?这些 for 循环有时会很棘手,不是吗? :)
:)
That's a great first post, by the way.顺便说一句,这是一篇很棒的第一篇文章。 Having a Minimal Reproducible Example (MRE, also called reprex or MCVE [Minimal, Complete, Verifiable Example]) always help others to quickly verify, debug and solve your problem.
拥有最小可重现示例(MRE,也称为 reprex 或 MCVE [最小、完整、可验证示例])总是可以帮助其他人快速验证、调试和解决您的问题。
public class DivisibleByThree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = Integer.valueOf(scanner.nextLine());
int b = Integer.valueOf(scanner.nextLine());
divisibleByThreeInRange(a, b);
}
public static void divisibleByThreeInRange(int beginning, int end) {
for (int i = beginning ; i <= end; i++) {
if (i % 3 == 0) {
System.out.println(i);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.