Online Compiler C

#include <stdio.h> int main() { int n; scanf("%d",&n); int count=0; while(n>0){ int current_digit=n%10; if(current_digit==9){ count++; } n/10; } if(count==1){ printf("YES"); }else{ printf("NO"); } return 0; }
这段代码的目的是判断一个整数中是否恰好包含一个数字9。程序读取一个整数,然后通过循环逐位检查每一位数字,统计数字9出现的次数。如果恰好出现一次,输出"YES",否则输出"NO"。

代码中存在一个逻辑错误,导致循环无法正确更新n的值,从而陷入无限循环或无法正确遍历所有位。

- 在循环内部,你写了 `n/10;` 这一行,但这一行只是计算了n除以10的结果,并没有将结果赋值回n。因此n的值永远不会改变,循环会一直检查同一个数字。你需要将n除以10的结果保存回n,例如使用 `n = n / 10;` 或 `n /= 10;`。

- 另外,注意变量命名的一致性:代码中使用了 `current_digit`,但后面又出现了 `current_digit` 和 `current_digit` 的拼写不一致(实际上这里是一致的,但请检查是否有笔误)。确保变量名拼写正确。