本帖最后由 zhangjinxuan 于 2022-11-17 16:14 编辑
你的程序思路不是很对,重写了一下:
- #include <stdio.h>
- int main() {
- char b;
- int find_number = 0;
- while ((b = getchar()) != '\n') {
- if (b >= '0' && b <= '9') {
- printf("%c", b);
- find_number = 1;
- } else {
- if (find_number) return 0;
- }
- }
- return 0;
- }
复制代码
就是说先记一个变量,遇到数字把变量设置为真,如果不是数字,并且遇到过数字,说明第一个数字已经读完了,输出完了,就不需要执行下去了,直接退出
你的代码因为新的一轮循环就已经把 b 给覆盖掉了,所以你在 b = '\n' 也没有任何的作用
如果要在你的代码基础上改的话,可以这么改,不过因为思路不是很对,所以也难免有错:
- #include<stdio.h>
- int main()
- {
- int a,c;
- int b;
- a=0;
- while((b=getchar())!='\n')
- {
- if(b>='0'&&b<='9')
- {
- printf("%c",b);
- c=getchar();
- if(c>'9')
- {
- b='\n';
- }
- else
- {
- printf("%c",c);
- }
-
-
- }
- if(b=='\n') //判断条件改一改就可以了
- {
- break;
- }
-
- }
- return 0;
- }
复制代码
还有啊,代码注释应该是 //,不是 \\,你的代码那两个地方写错了