| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3386 人关注过本帖
标题:为什么我的while循环跳不出来.....
只看楼主 加入收藏
shujie198
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2017-3-6
结帖率:100%
收藏
 问题点数:0 回复次数:5 
为什么我的while循环跳不出来.....
#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编辑过]

2017-03-07 23:24
weidelong
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:31
专家分:158
注 册:2015-2-6
收藏
得分:0 
while (scanf("%d",&x)!='\n')这句有问题
你的原意可能是输入回车就退出,但这里x接收到的是十进制的ascii码,建议你改成while (scanf("%c",&x)!='q'),输入Q就跳出。
另外,帖子最好加点分,不然没人
2017-03-28 15:54
zjchdn
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-2-24
收藏
得分:0 
这个n好像和d%没半毛钱关系吧
2017-04-07 17:03
hhzhuzw
Rank: 2
等 级:论坛游民
帖 子:3
专家分:10
注 册:2016-5-26
收藏
得分:0 
仔细阅读一下scanf函数,一定出在返回值上
2017-08-13 21:20
大漠苍穹
Rank: 1
等 级:新手上路
威 望:1
帖 子:11
专家分:0
注 册:2016-7-27
收藏
得分:0 
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.

2017-08-14 00:17
农民工
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:21
帖 子:109
专家分:639
注 册:2015-8-22
收藏
得分:0 
楼主理解下下面的句子
Both scanf and wscanf return the number of fields successfully converted and assigned
2017-09-21 14:24
快速回复:为什么我的while循环跳不出来.....
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056702 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved