FizBuzz程序:如何使输出正确?
我有一个关于这个程序的问题,它说:FizzBuzz 挑战:显示从 1 到 x 的数字,将 3 的倍数替换为“fizz”,将 5 的倍数替换为“buzz”,将 5 的倍数替换为“fizzbuzz” 3 和 5。结果必须是:1 2 fizz 4uzz fizz 7 8 fizzuzz 11 fizz 13 14 fizzbuzz 16 ...
所以我的问题是在打印输出时,我不知道要做什么做。
public class Multiplos {
public static void main(String args[]) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) {
System.out.print(i + " ");
System.out.print(" fizz ");
}
if (i % 5 == 0) {
System.out.print(" " + i);
System.out.print(" " + "buzz ");
}
if((i % 3 == 0)&&(i % 5 == 0)){
System.out.print(i + " ");
System.out.print(" fizzbuzz ");
}
}
}
}
I got a question about this program, it says: The FizzBuzz Challenge: Display numbers from 1 to x, replacing the word 'fizz' for multiples of 3, 'buzz' for multiples of 5 and 'fizzbuzz' for multiples of both 3 and 5. Th result must be:1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz 16 ...
So my problem is at the time to print the output, I dont know what to do.
public class Multiplos {
public static void main(String args[]) {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0) {
System.out.print(i + " ");
System.out.print(" fizz ");
}
if (i % 5 == 0) {
System.out.print(" " + i);
System.out.print(" " + "buzz ");
}
if((i % 3 == 0)&&(i % 5 == 0)){
System.out.print(i + " ");
System.out.print(" fizzbuzz ");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是伪代码:
我将把它作为练习,让您将其转换为 Java,因为这可能有助于理解其工作原理。
Here's the pseudocode:
I'll leave it as an exercise for you to convert it into Java, as that might help with the understanding as to how this works.
问题当然是,当
(i % 3 == 0)&&(i % 5 == 0)
为 true 时,前面的两个条件也为 true,因此您会得到重复的输出。解决此问题的最简单方法是检查前两种情况下的其他条件是否不成立。即,使第一个条件if((i % 3 == 0)&&(i % 5 != 0))
和第二个条件相同。代码的另一个问题是,当任何一种情况为真时,您都会打印数字,但当没有一种情况为真时,您应该打印它。您可以通过创建第四个 if 条件来解决此问题,该条件检查是否没有任何条件为 true,如果是,则打印 i。
现在,如果您执行了上述操作,您会发现最终出现了一些代码重复。如果您稍微想一下,您会发现可以通过使用 if - else if - else if - else 轻松解决这个问题,这允许您在检查当前条件时假设先前的条件为假。
The problem is of course that when
(i % 3 == 0)&&(i % 5 == 0)
is true, the two preceding conditions are also true, so you get duplicated output. The easiest way to fix that is to check that the other condition is not true in the first two cases. I.e. make the first conditionif((i % 3 == 0)&&(i % 5 != 0))
and the same for the second.The other problem with your code is that you're printing the number when any of the cases is true, but you're supposed to print it when none of them are. You can fix that by making a fourth if-condition which checks that none of the conditions are true and if so, prints i.
Now if you did the above, you'll see that you ended up with some code duplication. If you think about it a bit, you'll see that you can easily fix that, by using if - else if - else if - else, which allows you to assume that the previous conditions were false when the current condition is checked.
嗯,我想我只会提示:
else if
语句。Hm, I think I'll only hint:
else if
statement.使用
else if
以使条件不会重叠。Use
else if
so that the conditional don't overlap.