作者zptdaniel ()
看板TransCSI
标题Re: [问题] 一个C语言问题
时间Tue Jul 7 18:46:28 2009
※ 引述《Asbarla (木头)》之铭言:
: 看过版规还是问一下: 不好意思 这里可以问程式码问题吗
: 我练习C语言遇到一个小问题
: 题目是这样: 请撰写程式为输入的一连串整数加总,
: 假设scanf读到的第一个数代表接下来要输入的个数。
: 这是我的主程式片段程式码:
: int i = 1, j, k;
: printf("请输入你想要输入数字的个数: ");
: scanf("%d",&j);
: while(i < j+1)
: {
: printf("请输入第 %d 个数: ",i);
: scanf("%d",&k);
: ++i;
: }
: printf("总和: \n",&k);
: 请问要怎麽为我在while里面输入的数字做加总呢
: 想好久,想不出来。请益各位大大们谢谢。
我打code,标头档跟结尾就省略了.
int number,count=0,sum=0,input;
printf("请输入你想要输入数字的个数:");
scanf("%d",&number);
while(count++<number)
{
printf("请输入第%2d个整数:",count);
scanf("%d",&input);
sum += input;
}
printf("总和为 %d",sum);
--
这流浪的人,努力让梦成真。
That's the way I live, that's the way I did.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.194.100.216
※ 编辑: zptdaniel 来自: 123.194.100.216 (07/07 18:48)
1F:推 Asbarla:多设一个变数就可以解决了! 谢谢提醒! 07/07 20:35