需要如果构建建议

发布于 2024-12-23 18:16:25 字数 477 浏览 2 评论 0原文

我有一个 for 循环,其中放置了几个 if 语句。这些条件的目的是检查数字的整除性,如果该数字可以被 3 整除,则输出一个字符串。如果该数字可以被 5 整除,则将输出另一个字符串。但是,如果该数字可以同时被 3 和 5 整除,则将在其位置输出完全不同的字符串,而不是其他字符串。

这是我的代码:

for (i = 1; i <= file_int; i++){
     if (i % 3 == 0) {
        printf("Hoppity \n");
    }
    if (i % 5 == 0) {
        printf("Hophop \n");
    }
    if (i % 5 == 0 && i % 3 == 0) {
        printf("Hop \n");
    }   
}

如您所见,最后一个条件不太有效。我应该使用什么类型的控制结构?别的?

多谢。

I have a for loop in which I placed several if statements. The objective of these conditionals is to check divisibility of a number and then output a string if the number is divisible by 3. If the number is divisible by 5, another string will be outputted. However, if the number is divisible by both 3 and 5, an entirely different string will be outputted in its place instead of the other strings.

Here is my code:

for (i = 1; i <= file_int; i++){
     if (i % 3 == 0) {
        printf("Hoppity \n");
    }
    if (i % 5 == 0) {
        printf("Hophop \n");
    }
    if (i % 5 == 0 && i % 3 == 0) {
        printf("Hop \n");
    }   
}

As you can see, the last conditional doesn't quite work. What type of control construct should I use? else?

Thanks alot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凯凯我们等你回来 2024-12-30 18:16:25
for (i = 1; i <= file_int; i++){
    if (i % 5 == 0 && i % 3 == 0) {
        printf("Five and three\n");
    } else if (i % 3 == 0) {
        printf("Three\n");
    } else if (i % 5 == 0) {
        printf("Five\n");
    } else {
        printf("None of the conditions passed\n");
    }
}
for (i = 1; i <= file_int; i++){
    if (i % 5 == 0 && i % 3 == 0) {
        printf("Five and three\n");
    } else if (i % 3 == 0) {
        printf("Three\n");
    } else if (i % 5 == 0) {
        printf("Five\n");
    } else {
        printf("None of the conditions passed\n");
    }
}
愿得七秒忆 2024-12-30 18:16:25

我会使用 else-ifs 并让我们知道

(i % 5 == 0 && i % 3 == 0) <=> (i % 15 == 0)

for (i = 1; i <= file_int; i++){
  if (i % 15 == 0)
    printf("Hop \n");
  else if (i % 3 == 0)
    printf("Hoppity \n");
  else if (i % 5 == 0)
    printf("Hophop \n"); 
}

当然,除了 for 循环之外,您也可以不使用任何控制结构就可以逃脱:

const char* values[15] = {"Hop \n", "", "", "Hoppity \n", "", 
                          "Hophop \n", "Hoppity \n", "", "", "Hoppity \n", 
                          "Hophop \n", "", "Hoppity \n", "", ""};
for (int i = 1; i <= 100; i++) 
  printf(values[i % 15]);

对于这个例子来说,这个解决方案有点疯狂,但是它展示了如何以不同的方式做事(并且在编写代码时,在一个函数中永远不会有超过一定数量的分支路径(过度热心的编码约定......))。

I would use else-ifs and make us of the fact that

(i % 5 == 0 && i % 3 == 0) <=> (i % 15 == 0):

for (i = 1; i <= file_int; i++){
  if (i % 15 == 0)
    printf("Hop \n");
  else if (i % 3 == 0)
    printf("Hoppity \n");
  else if (i % 5 == 0)
    printf("Hophop \n"); 
}

Of course you can also get away without using any control structures except the for-loop at all:

const char* values[15] = {"Hop \n", "", "", "Hoppity \n", "", 
                          "Hophop \n", "Hoppity \n", "", "", "Hoppity \n", 
                          "Hophop \n", "", "Hoppity \n", "", ""};
for (int i = 1; i <= 100; i++) 
  printf(values[i % 15]);

That solution is slightly insane for this example, but it shows how you can do things differently (and it's not so farfetched when writing code where you shall never ever have more then a certain number of branch paths in one function (overzealous coding conventions...)).

待"谢繁草 2024-12-30 18:16:25

只是为了它,而不推荐它,因为它可能更难阅读,因为它滥用从 boolint 的转换:

int msg = (i % 3 == 0) + 2*(i % 5 == 0);
switch ( msg ) {
case 3:
   cout << "Multiple of 3 and 5";
case 2: 
   cout << "Multiple of 5";
case 1:
   cout << "Multiple of 3";
}

可以进一步压缩为

const char* msgs[] = { "", "Multiple 3", "Multiple 5", "Multiple 3 and 5" };
cout << msgs[ (i%3==0) + 2*(i%5==0) ];

:当然,这两种解决方案都违背了问题本身,因为它们不是 if 构造,而是避免在第一种情况下使用 if分支 一般在第二种情况下。

Just for the sake of it, and not recommending it, as it can be harder to read as it abuses conversions from bool to int:

int msg = (i % 3 == 0) + 2*(i % 5 == 0);
switch ( msg ) {
case 3:
   cout << "Multiple of 3 and 5";
case 2: 
   cout << "Multiple of 5";
case 1:
   cout << "Multiple of 3";
}

which can be further condensed into:

const char* msgs[] = { "", "Multiple 3", "Multiple 5", "Multiple 3 and 5" };
cout << msgs[ (i%3==0) + 2*(i%5==0) ];

Of course, both solutions are against the question itself, as they are not if constructs, but rather avoid the use of if in the first case, and branches in general in the second case.

汐鸠 2024-12-30 18:16:25

另一种解决方案,更接近您的原始代码。尽管 else 解决方案确实更高效(也更优雅)。

for (i = 1; i <= file_int; i++){
    if (i % 3 == 0 && i % 5 != 0) {
        printf("Hoppity \n");
    }
    if (i % 5 == 0 && i % 3 != 0) {
        printf("Hophop \n");
    }
    if (i % 5 == 0 && i % 3 == 0) {
        printf("Hop \n");
    }   
}

An alternative solution, keeping closer to your original code. Although the else solution is indeed more efficient (and elegant).

for (i = 1; i <= file_int; i++){
    if (i % 3 == 0 && i % 5 != 0) {
        printf("Hoppity \n");
    }
    if (i % 5 == 0 && i % 3 != 0) {
        printf("Hophop \n");
    }
    if (i % 5 == 0 && i % 3 == 0) {
        printf("Hop \n");
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文