1   1  /  1  页   跳转

写的C程序,有问题请教.

写的C程序,有问题请教.

main()
{
float x;
float y;
printf("please input x:");
scanf("%f",&x);
if(x<0.0)
  y=0;
else if(x>0&&x<=10)
y=x;
else if(x>10&&x<=20)
y=10;
else if(x>20&&x<40)
y=-0.5*x+20;
else if(x>=40)
y=-1;
if(y!=-1)
printf("%f",y);
else
printf("error\n");
getch();
}


写了这样一个程序,当我输入一个数据时,执行结果是正确的.但是只能输入一次啊,假如再按任何键就会退出.我想知道用什么办法,可以让我输入无数次,也就是要它退出的时候,它才退出.
最后编辑2006-10-08 20:10:12
分享到:
gototop
 

main()
{
float x;
float y;
char flag=0;

do
{
printf("please input x:");
scanf("%f",&x);
if(x<0.0)
y=0;
else if(x>0&&x<=10)
y=x;
else if(x>10&&x<=20)
y=10;
else if(x>20&&x<40)
y=-0.5*x+20;
else if(x>=40)
y=-1;
if(y!=-1)
printf("%f",y);
else
printf("error\n");
getch();

printf("Do you want to exit(Y/N)?");
scanf("%c",&flag);

if(flag=='Y') break;
}while(1);
}
我想知道while(1)里面的1是怎么样出来的。
gototop
 

还有,我运行了天下奇才的程序,虽然输完一个程序后会出现do you want to exitY/N,但是还后还紧跟着please input the x:
为什么会这样?
gototop
 

printf("Do you want to exit(Y/N)?");
scanf("%c",&flag);

if(flag=='Y') break;

我这你写的上面的三个语句删掉,运行程序,输入X,然后出现结果。但是要等我按一下键盘才会再次出现please input the x:这句话。为什么?
gototop
 
1   1  /  1  页   跳转
页面顶部
Powered by Discuz!NT