鱼C论坛

 找回密码
 立即注册
查看: 1626|回复: 1

为什么输入3452时输出与预期不符

[复制链接]
发表于 2023-10-30 11:06:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <math.h>
int main()
{
int a;
scanf("%d",&a);
int x,y;
y = a;
do{
x = a % 10;
a /= 10;
if(x == 5||x==9){
printf("%d",y%45);
return 0;
}
else
printf("Not include!");
}while(x == 0);
   return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-30 11:06:11 | 显示全部楼层
问题的原因在于代码中的循环条件不正确,导致在输入3452时输出与预期不符。

在代码中,循环条件  while(x == 0)  应该改为  while(a != 0) ,以确保在 a 不等于 0 的情况下继续循环。

修改后的代码如下:

  1. #include <stdio.h>
  2. #include <math.h>

  3. int main()
  4. {
  5.     int a;
  6.     scanf("%d", &a);
  7.     int x, y;
  8.     y = a;
  9.     do {
  10.         x = a % 10;
  11.         a /= 10;
  12.         if (x == 5 || x == 9) {
  13.             printf("%d", y % 45);
  14.             return 0;
  15.         } else {
  16.             printf("Not include!");
  17.         }
  18.     } while (a != 0);
  19.    
  20.     return 0;
  21. }
复制代码


这样修改后,输入3452时将输出 "Not include!",符合预期结果。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 00:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表