while循环里面scanf_4.1 循环

#include <stdio.h>

int main(){
 
 int x;
 int n = 0;
 scanf("%d",&x);
 n++;
 x/=10;
 while (x>0){
  n++;
  x /= 10;
 }
 printf("%dn",n);
 return 0;
}


while循环

  • 如果我们把while翻译作“当”,那么一个while循环的意思就是:当条件满足时,不断地重复循环体内的语句
  • 循环执行之前判断是否继续循环,所以有可能循环一次也没有被执行;
  • 条件成立是循环继续的条件

验证

  • 测试程序常使用边界数据,如有效范围两端的数据、特殊的倍数等
  • 个位数
  • 10
  • 0
  • 负数

do=while循环

  • 在进入循环的时候不做检查,而是在执行完一轮循环体的代码之后,再来检查循环的条件是否满足,如果满足则继续下一轮循环,不满足则循环结束

两种循环

  • do-while循环和while循环很像,区别是在循环体执行结束的时候才来判断条件。也就是说,无论如何,循环都会执行至少一遍,然后再来判断条件。与while循环相同的是,条件满足时执行循环,条件不满足时结束循环
  • while结尾要有分号 int x; scanf("%d",&x); int n = 0; do { x /= 10; n ++; }while (x>0); printf("%d",n);

随机数

  • 每次召唤rand()就得到一个随机的整数

%100

  • x % n的结果是[0,n-1]的一个整数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
scanf_count小于等于usart_count,则scanf_count_old==usart_count。 以下是比较函数的代码: ```c int compare(int scanf_count, int usart_count, int scanf_count_old) { if (usart_count == 0) { if (scanf_count > scanf_count_old) { scanf_count_old = scanf_count; } } else { if (scanf_count > usart_count) { printf("设备没有取完\n"); } else { scanf_count_old = usart_count; } } return scanf_count_old; } ``` 使用方法: 1. 将以上代码放入你的程序中。 2. 调用`compare()`函数,传入三个变量(`scanf_count`、`usart_count`、`scanf_count_old`),并接收返回值。 ```c scanf_count_old = compare(scanf_count, usart_count, scanf_count_old); ``` 注意,`scanf_count_old`需要在调用函数之前定义并初始化。 完整示例: ```c #include <stdio.h> int compare(int scanf_count, int usart_count, int scanf_count_old); int main() { int scanf_count = 5; int usart_count = 0; int scanf_count_old = 0; scanf_count_old = compare(scanf_count, usart_count, scanf_count_old); printf("scanf_count_old = %d\n", scanf_count_old); usart_count = 6; scanf_count_old = compare(scanf_count, usart_count, scanf_count_old); printf("scanf_count_old = %d\n", scanf_count_old); scanf_count = 7; scanf_count_old = compare(scanf_count, usart_count, scanf_count_old); printf("scanf_count_old = %d\n", scanf_count_old); return 0; } int compare(int scanf_count, int usart_count, int scanf_count_old) { if (usart_count == 0) { if (scanf_count > scanf_count_old) { scanf_count_old = scanf_count; } } else { if (scanf_count > usart_count) { printf("设备没有取完\n"); } else { scanf_count_old = usart_count; } } return scanf_count_old; } ``` 输出结果: ``` scanf_count_old = 5 scanf_count_old = 6 scanf_count_old = 7 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值