c - 我的程序不会停止执行 if 语句但条件不满足
问题描述
它应该是 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;
}
}}
解决方案
该if
语句使您执行或不执行块。
例如,在您的代码中:
if (check <= 6){
res = mult * check;
printf("%d ",res);
check ++;
}
if (check > 6 ){
printf("\n ");
int check = 0;
}
第一个块将在 when 执行check <= 6
,第二个块将执行check > 6
... 但while
循环中的条件是mult != 5
... 并且您永远不会修改mult
,因此条件始终为真。
因此,除了 uZuMaKioBaRuTo 对check
变量的注释之外,您还需要递增mult
:
if (check > 6 ){
printf("\n ");
check = 0;
mult++;
}
推荐阅读
- reactjs - 如何使用自动 y 值插入注释
- excel - 寻找在单个单元格中每个句子的开头添加“子弹指针”的公式
- python - 计算有限制的字符串的子字符串
- sql - 通过自连接同一表进行计数,其中一个字段为空,但另一个字段具有值
- apache - 无法在 Apache 2.4 中取消设置升级标头
- swift - 如何在 Swift 中读取 Firestore 时间戳
- regex - 将正则表达式与 grep 输出进行比较 - bash 脚本
- jenkins - 从计算引擎 vm 上的 Jenkins 脚本管道中的谷歌容器注册表中提取
- javascript - Javascript 点击选项不会将其下拉 - Chrome 扩展
- imagej - 如何从 Image J Pendant Drop 插件的结果表中导出数据?