菜鸟,求解惑——关于while循环的标志值

Woodz 2010-10-09 05:10:36
为什么输入 -1 后还会执行循环里的内容呢……


#include <stdio.h>

main()
{
float grade, counter, total,average;
counter = 0, total = 0;


while (grade != -1)
{
printf("Enter -1 to end, please enter grade: ");
scanf("%f", &grade);
counter = counter + 1;
total = total + grade;
}
++ total, -- counter;
average = total / counter;

printf("The counter is %f, The average is %f.\n", counter, average);
}

...全文
189 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2010-10-11
  • 打赏
  • 举报
回复
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
whlie (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。
wizard_tiger 2010-10-11
  • 打赏
  • 举报
回复
2楼3楼正解。
精度问题。
Defonds 2010-10-10
  • 打赏
  • 举报
回复
精度。
Jesusgospelnj 2010-10-10
  • 打赏
  • 举报
回复
有两个问题:1.grade没有初始化,不同的编译器会有不同的处理,所以会进入while循环。2.浮点数比较的问题。不可以直接比较,必须给出精度。
zhengkaimiao 2010-10-10
  • 打赏
  • 举报
回复
第一次进入循环的时候还没有grade 值~~
Woodz 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 free4537 的回复:]
为什么输入 -1 后还会执行循环里的内容呢……

C/C++ code

#include <stdio.h>

main()
{
float grade, counter, total,average;
counter = 0, total = 0;


while (grade != -1)
{
printf("Enter -1 to en……
[/Quote]
可能大家没有明白我的意思,我是想当输入 -1 时不执行while内的语句,从而将末尾的 -- ++ 删了
Woodz 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 shenglongfei 的回复:]

精度问题啊兄弟,在计算机中浮点型和整数型变量是不一样的意义,你的grade要定义为整形应该就可以啦
[/Quote]

试了,不可以……
千杯不醉-sen 2010-10-09
  • 打赏
  • 举报
回复
2楼3楼正解。
shenglongfei 2010-10-09
  • 打赏
  • 举报
回复
精度问题啊兄弟,在计算机中浮点型和整数型变量是不一样的意义,你的grade要定义为整形应该就可以啦
Woodz 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 udbbjwxf 的回复:]
后面还有语句吧
[/Quote]
没了,就这些
Woodz 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jim_king_2000 的回复:]
浮点数是不精确的,无法判断相等或不等。只能判断大小。

C/C++ code
while (abs(grade + 1) < 10e-5)
[/Quote]
那为什么输入 -1 后会停止循环呢
Woodz 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 baihacker 的回复:]

C/C++ code

//精度原因。
//我计算机上用grade != -1木有问题
#include <stdio.h>
#include <math.h>
main()
{
float grade, counter, total,average;
counter = 0, total = 0;


while (fabs(grade + 1) > 1e-6)
……
[/Quote]
你是指结果没有问题?用我贴的代码我这也没有问题啊,因为我后边用了 ++ 和 -- 了
udbbjwxf 2010-10-09
  • 打赏
  • 举报
回复
后面还有语句吧
Woodz 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 petewong 的回复:]

grade 定义成 int
然后再看 还会不
[/Quote]
不行啊, 还是 counter 多1, total 少1
Jim_King_2000 2010-10-09
  • 打赏
  • 举报
回复
浮点数是不精确的,无法判断相等或不等。只能判断大小。
while (abs(grade + 1) < 10e-5)
baihacker 2010-10-09
  • 打赏
  • 举报
回复

//精度原因。
//我计算机上用grade != -1木有问题
#include <stdio.h>
#include <math.h>
main()
{
float grade, counter, total,average;
counter = 0, total = 0;


while (fabs(grade + 1) > 1e-6)
{
printf("Enter -1 to end, please enter grade: ");
scanf("%f", &grade);
counter = counter + 1;
total = total + grade;
}
++ total, -- counter;
average = total / counter;

printf("The counter is %f, The average is %f.\n", counter, average);
}
petewong 2010-10-09
  • 打赏
  • 举报
回复
grade 定义成 int
然后再看 还会不

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧