打印从 1 到 100 的数字并用字符串替换一些数字 (fizzbuzz)
我正在从这里尝试 fizzbuzz 程序: 为什么程序员不能.. 编程?< /a>
"编写一个程序,打印从 1 到 100 的数字。但是对于三的倍数,打印“Fizz”而不是数字;对于五的倍数,打印“Buzz”。对于同时是三和五的倍数的数字打印“菲兹巴兹”。”
protected void btn1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 101; i++)
{
if (i % 3 == 0 & i % 5 == 0)
{
Response.Write("fizzbuzz" + ",");
}
else if (i % 3 == 0)
{
Response.Write("fizz" + ",");
}
else if (i % 5 == 0)
{
Response.Write("buzz" + ",");
}
else
{
i = i + 0;
}
Response.Write(i +",");
}
}
我能够产生某种结果,例如:
1,2,嘶嘶声,3,4,嗡嗡声,5,嘶嘶声,6,7,8,嘶嘶声,9,嗡嗡声,10,11,嘶嘶声,12,13,14,嘶嘶声,15,16,17 ,嘶嘶声,18,19,嗡嗡声,20,嘶嘶声,21,22,23,嘶嘶声,24,嗡嗡声,25,26,嘶嘶声,27,28,29,嘶嘶声,30,31,32,嘶嘶声,33,34 、嗡嗡声、35、嘶嘶声、36、37、38、嘶嘶声、39 等等..
打印了 fizz 一词,但它没有取代 3,并且打印了 fizzbuzz,但它没有替换 15 等等...
I am trying the fizzbuzz program from here: Why Can't Programmers.. Program?
"Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"."
protected void btn1_Click(object sender, EventArgs e)
{
for (int i = 1; i < 101; i++)
{
if (i % 3 == 0 & i % 5 == 0)
{
Response.Write("fizzbuzz" + ",");
}
else if (i % 3 == 0)
{
Response.Write("fizz" + ",");
}
else if (i % 5 == 0)
{
Response.Write("buzz" + ",");
}
else
{
i = i + 0;
}
Response.Write(i +",");
}
}
I am able to produce some kind of result like:
1,2,fizz,3,4,buzz,5,fizz,6,7,8,fizz,9,buzz,10,11,fizz,12,13,14,fizzbuzz,15,16,17,fizz,18,19,buzz,20,fizz,21,22,23,fizz,24,buzz,25,26,fizz,27,28,29,fizzbuzz,30,31,32,fizz,33,34,buzz,35,fizz,36,37,38,fizz,39, and so on..
The word fizz was printed but it did not replace 3 and fizzbuzz was printed but it did not replace 15 and so ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
无论您是否满足 if 条件,您仍然会在代码末尾打印
i
。特别看看你的 for 循环:
所以你需要移动最后一个 Response.Write(i + ",");在最后一个
else
条件中。查找此类错误的最简单方法是使用调试器并调试程序。然后您将很容易看到输出是什么。因此,一定要使用调试器并设置断点/监视并观察会发生什么。您的代码应更改为:请注意,您的
for
循环已通过增加 i 来处理此问题,从而删除了i=i+1
。编辑
Not sure if this is easier but here is another way to do this using lambda's:
Whether you hit the if condition or not you are still printing
i
at the end of your code.Look specifically at your for loop:
So you need to move that last Response.Write(i + ","); in the last
else
condition. The easiest way to find bugs like these is to use the debugger and debug your program. You will then easily see what the output is. So definately use the debugger and set breakpoints / watches and watch what happens. Your code should change to this:Notice the removal of
i=i+1
yourfor
loop is handling this already by incrementing i.Edit
Not sure if this is easier but here is another way to do this using lambda's:
尝试这些更改
您的
i = i + 0
显然没有执行任何操作,因为您将 0 添加到 i 的值。而且无论 if/else 块的结果如何(放在它后面),您都会将数字打印到响应中,因此应该将其移至 else 中(意味着仅在 if 或 else if 不匹配时才打印。
Try these changes
Your
i = i + 0
obviously does nothing, since you are adding 0 to the value of i.And you are printing the number to the response regardless of the result of the if/else block (it's put after it), so it should be moved into else (meaning only print if the if, or else if did not match.
将 Response.Write(i +","); 移动到最后的 else 中
move
Response.Write(i +",");
into your final else它的另一个简单实现:
Another simple implementation of it:
这是我最初的解决方案...
但这一个要短得多...
Here was my original solution...
But this one is much shorter...