简体   繁体   English

C++:停止我的 Fizz Buzz 程序中的最后一个增量

[英]C++: stopping last increment in my Fizz Buzz program

So as part of a coding challenge I tried to make a Fizz Buzz program in C++ without looking at the solution.因此,作为编码挑战的一部分,我尝试在 C++ 中制作 Fizz Buzz 程序,而无需查看解决方案。 For those of you who don't know, it should be a loop that replaces any number divisible by 3 with Fizz , any number divisible by 5 with Buzz , and any number divisible by both with FizzBuzz :对于那些不知道的人,它应该是一个循环,用Fizz替换任何能被 3 整除的数字,用Buzz替换任何能被 5 整除的数字,用FizzBuzz替换任何能被两者整除的数字:

1
2
Fizz
4
Buzz
6
7
8
Fizz
Buzz
11
Fizz

I'm almost there with the code below, however, i'm a little annoyed that even though I want the loop to stop entirely at 100, the way i've set up the program means that an extra 1 gets added to i after the loop has ended.我几乎用下面的代码在那里,但是,我有点恼火,即使我希望循环完全停止在 100,我设置程序的方式意味着额外的 1 被添加到i之后循环已经结束。 Is there a way of stopping my FizzBuzz program from going past 100?有没有办法阻止我的 FizzBuzz 程序超过 100?

#include <iostream>

using namespace std;

int main () {

for (int i = 1; i < 100; ++i){

if (i % 3 == 0 && i % 5 == 0){
    cout << "FizzBuzz\n";
    i = i + 1;
}

if (i % 3 == 0){
  cout << "Fizz\n";
  i = i + 1;
}

if (i % 5 == 0){
  cout << "Buzz\n";
  i = i + 1;
}

cout << i << "\n";

}

}

So I fixed your code a little bit:所以我稍微修正了你的代码:

int main()
{
    for (int i = 1; i < 101; ++i)
    {
       if (i % 3 == 0 && i % 5 == 0)
            cout << "FizzBuzz\n";
        else if (i % 3 == 0)
            cout << "Fizz\n";
        else if (i % 5 == 0)
            cout << "Buzz\n";
        else
            cout << i << "\n";

    }

}

Every time you do i = i + 1;每次你做i = i + 1; , it's kinda useless because your loop does that. ,这有点没用,因为您的循环会这样做。 Also, I put everything is an if else chain, instead of and if if chain.另外,我把一切都是if else链,而不是if if链。 That way only on statement will execute at any giving time.这样只有 on 语句才会在任何给定时间执行。 Also changed the max to 101 instead of 100 since the for loop will stop at 101 and not print the result of 101.还将最大值更改为 101 而不是 100,因为 for 循环将在 101 处停止并且不打印 101 的结果。

Hope this helps:)希望这可以帮助:)

In c++, variables have a lifetime , called their scope, and after their lifetime expires they are deleted.在 c++ 中,变量有一个生命周期,称为它们的 scope,在它们的生命周期到期后它们被删除。 The i that is defined in the loop declaration has the scope constrained by the closing brace "}" of the for loop.在循环声明中定义的 i 的 scope 受 for 循环的右大括号“}”约束。 Due to this, the variable i does not exist after the loop, only during.因此,变量 i 在循环之后不存在,仅在循环期间存在。 However in the code you have provided, the problem is that you are incrementing i multiple times.但是,在您提供的代码中,问题是您多次递增 i 。 In your for loop you increment i every time the body of the for loop is executed, but within that body you increment i when it is divisible by 5 or 3 or both or neither.在您的 for 循环中,每次执行 for 循环的主体时都会增加 i ,但是在该主体中,当 i 可被 5 或 3 或两者都整除或两者都不能整除时,您会增加 i 。 You can remove all of the i = i + 1;您可以删除所有 i = i + 1; statements and remove most of the bugs.声明并删除大部分错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM