| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 611 人关注过本帖
标题:[求助]刚学C 这个问题搞不懂了啊
只看楼主 加入收藏
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
 问题点数:0 回复次数:7 
[求助]刚学C 这个问题搞不懂了啊

#include "stdio.h"

main() { int a=0,a1; float b=0,b1; char c; do{ scanf("%c%d%f",&c,&a1,&b1); if(c=='s'){a+=a1;b+=b1;} if(c=='b'){a-=a1;b-=b1;} printf("%c %d %5.2f\n",c,a,b);} while(c!='e'); } 当第一次输入数据时没错 第二次以后每次输入数据都会打印出2个结果 这是怎么回事啊 哪位好心人指点一下 比如 输入s 1 11 输出s 1 11.00 再输入s 2 12

输出 1 11.00 s 3 23.00 再输入s 3 11

输出 3 23.00 s 6 34.00

搜索更多相关主题的帖子: include 
2005-04-25 02:08
lymlyc
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-4-24
收藏
得分:0 
不会吧,我看你这个程序都有问题,不应该能运行的。
2005-04-25 08:17
lymlyc
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-4-24
收藏
得分:0 
a,b,在不断的累加中,变量记住了上次的结果,你在输入相同当然不一样了

要对a,b进行清空

必须重新运行,输入同样的,数据,如果那时候结果不一样,那就是你的程序有问题
那是因为你有判断的输出啊

scanf("%c%d%f",&c,&a1,&b1);
if(c=='s'){a+=a1;b+=b1;}
if(c=='b'){a-=a1;b-=b1;}
printf("%c %d %5.2f\n",c,a,b);} while(c!='e');
}
当你输入的字符是s的时候执行{a+=a1;b+=b1;}
当你输入的字符是b的时候执行{a-=a1;b-=b1;}
如果你输入的不是这两个的话,那肯定输出的上0了

细看看就明白了,不明白加我QQ84962947
2005-04-25 10:38
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
得分:0 
楼上的答非所问了吧

在循环中我只有一个scanf  对应一个printf  怎么会一次输入会有两次输出呢

还有 我每次都输入的字符都是s

/bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg');}" onmousewheel="return imgzoom(this);" alt="" /> 欢迎加入C语言QQ群698156 我们都是菜鸟乃至新手 坚信有一天定能展翅高飞 因为有着努力的决心 衷心盼望你的到来 让我们一起进步
2005-04-25 11:33
kenwuchan
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-3-31
收藏
得分:0 

这里主要是最后接收多了一个回车的字符,导致多了一次! 加个getchar()去接收这个字符就可! 程序改为: #include "stdio.h"

main() { int a=0,a1; float b=0,b1; char c; do{ scanf("%c%d%f",&c,&a1,&b1); getchar(); if(c=='s'){a+=a1;b+=b1;} if(c=='b'){a-=a1;b-=b1;} printf("%c %d %5.2f\n",c,a,b);} while(c!='e'); }

2005-04-25 12:26
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
得分:0 
嗯 谢谢楼上的了哈

[此贴子已经被作者于2005-4-25 13:32:17编辑过]



/bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg');}" onmousewheel="return imgzoom(this);" alt="" /> 欢迎加入C语言QQ群698156 我们都是菜鸟乃至新手 坚信有一天定能展翅高飞 因为有着努力的决心 衷心盼望你的到来 让我们一起进步
2005-04-25 13:31
musicml
Rank: 1
等 级:新手上路
帖 子:273
专家分:0
注 册:2005-4-2
收藏
得分:0 
//这里主要是最后接收多了一个回车的字符,导致多了一次! //可以使用清流函数fflush(stdin)来避免这个问题 #include "stdio.h"main() { int a=0,a1; float b=0,b1; char c; do{ scanf("%c%d%f",&c,&a1,&b1); fflush(stdin);//清流函数 if(c=='s'){a+=a1;b+=b1;} if(c=='b'){a-=a1;b-=b1;} printf("%c %d %5.2f\n",c,a,b);} while(c!='e'); }

Every thing is possible.
2005-04-25 16:43
Rank: 1
等 级:新手上路
帖 子:255
专家分:0
注 册:2005-4-25
收藏
得分:0 
狂晕 都说了刚学C 清流看不懂啊 还是要谢谢7楼的关心哈

就刚好看得懂5楼的

/bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=5&filename=2005-4/2005427111228529.jpg');}" onmousewheel="return imgzoom(this);" alt="" /> 欢迎加入C语言QQ群698156 我们都是菜鸟乃至新手 坚信有一天定能展翅高飞 因为有着努力的决心 衷心盼望你的到来 让我们一起进步
2005-04-25 16:54
快速回复:[求助]刚学C 这个问题搞不懂了啊
数据加载中...
 
   



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

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