注册 登录
编程论坛 VC++/MFC

为什么我的while循环跳不出来.....

shujie198 发布于 2017-03-07 23:24, 3735 次点击
#include<stdio.h>
int sushu(int a);
main()
{    int x;
while (scanf("%d",&x)!='\n')
if(sushu(x))
     printf("%d是一个素数\n",x);
else
     printf("%d不是一个素数\n",x);
}
int sushu(int a)
{int i;
for(i=2;i<=a/2;i++)
 if(a%i==0)
     return 0;
            return 1;
 }

[此贴子已经被作者于2017-3-8 00:26编辑过]

5 回复
#2
weidelong2017-03-28 15:54
while (scanf("%d",&x)!='\n')这句有问题
你的原意可能是输入回车就退出,但这里x接收到的是十进制的ascii码,建议你改成while (scanf("%c",&x)!='q'),输入Q就跳出。
另外,帖子最好加点分,不然没人
#3
zjchdn2017-04-07 17:03
这个n好像和d%没半毛钱关系吧
#4
hhzhuzw2017-08-13 21:20
仔细阅读一下scanf函数,一定出在返回值上
#5
大漠苍穹2017-08-14 00:17
scanf()的返回值是读入的数量,返回0代表没有读入,EOF代表文件尾或者读取错误
msdn上的说法:
Return Value

Both scanf and wscanf return the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.

#6
农民工2017-09-21 14:24
楼主理解下下面的句子
Both scanf and wscanf return the number of fields successfully converted and assigned
1