【问题标题】:My program won't stop executing the if statement yet the condition is not met我的程序不会停止执行 if 语句但条件不满足
【发布时间】:2021-12-16 00:14:10
【问题描述】:

它应该是 x 轴上从 1 到 6 和 y 轴上从 1 到 5 的乘法表,所以我有这个 if 语句应该在乘法达到 6 时跳转到 nezt 行但它保持即使我重置了其中要满足的条件,也会在执行时执行。

#include <stdio.h>
#include <stdlib.h>
int main(){
int mult = 1;
int check = 0;
int res;
while(mult != 6){


        if (check <= 6){
            res = mult * check;
            printf("%d ",res);
            check ++;
        }
        if (check > 6 ){
            printf("\n ");
            check = 0;
        }

}}

【问题讨论】:

  • int check = 0; 声明了一个新变量。试试check = 0;。你的循环永远不会结束,因为mult 永远不会改变。
  • 尝试调试你的代码并检查mul的值

标签: c if-statement variables while-loop


【解决方案1】:

if 语句使您执行或不执行块。

例如,在您的代码中:

if (check <= 6){
    res = mult * check;
    printf("%d ",res);
    check ++;
}
if (check > 6 ){
    printf("\n ");
    int check = 0;
}

第一个块将在check &lt;= 6 时执行,第二个将在check &gt; 6... 时执行,但while 循环中的条件是mult != 5... 并且您永远不会修改@987654327 @,所以条件始终为真。

所以,除了 uZuMaKioBaRuTo 对您的 check 变量的注释之外,您还需要增加 mult

if (check > 6 ){
    printf("\n ");
    check = 0;
    mult++;
}

【讨论】:

  • @TedLyngmo 我明白我的错误谢谢
猜你喜欢
  • 2023-04-09
  • 1970-01-01
  • 2023-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多