| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 504 人关注过本帖
标题:菜鸟求救
取消只看楼主 加入收藏
Tchong
Rank: 1
等 级:新手上路
帖 子:117
专家分:0
注 册:2008-10-17
结帖率:100%
收藏
 问题点数:0 回复次数:1 
菜鸟求救
我想编的程序是这样的:

Enter the gallons used(-1 to end):12.8

Enter the miles driven:287

The miles/gallon for this tank was 22.421875

 

Enter the gallons used(-1 to  end):10.3

Enter the miles driven:200

The miles /gallon for this tank was 19.417475

 

Enter the gallons used(-1 to end):5

Enter the miles driven :120

The miles/gallon for this tank was 24.000000

 

Enter the  galons used (-1 to end ): -1

The overall average miles /gallon was 21.601423

 

 

 

 

我所编的程序如下:

#include<stdio.h>
main()
{
float gallons,miles,result,average,counter,total;
 counter=0;
 total=0;
 while(gallons!=-1){
 printf("\nEnter the gallons used(-1 to end):");
 scanf("%f",&gallons);
 printf("\nEnter the miles driven:");
 scanf("%f",&miles);
 result=miles/gallons;
 counter=counter+1;
 printf("The miles/gallon for this tank was%f\n",result);}
 if(gallons=-1){
total=result+result;
average=(float)total/counter;
printf("\nThe overal average miles/gallon was%f",average);}
 }

请问这里有什么错误,我非常渴望知道自己的错误,因为我还是菜鸟。
2008-10-17 23:26
Tchong
Rank: 1
等 级:新手上路
帖 子:117
专家分:0
注 册:2008-10-17
收藏
得分:0 
我想到怎么做啦
应该是这样:#include<stdio.h>
main()
{
float gallons,miles,result,average,counter,total;
counter=0;
total=0;
miles=0;
gallons=0;
average=0;result=0;
printf("\nEnter the gallons used(-1 to end):");
scanf("%f",&gallons);
while(gallons>=0){
printf("Enter the miles driven:");
scanf("%f",&miles);
result=miles/gallons;
counter=counter+1;
total=total+result;
average=(float)total/counter;

printf("The miles/gallon for this tank was%f\n",result);
printf("\nEnter the gallons used(-1 to end):");
scanf("%f",&gallons);
}
if(gallons<0){
printf("\nThe overal average miles/gallon was%f\n",average);}
}



又或者是这样:#include<stdio.h>
int main()
{
float gallons,miles ,total,average,counter,Z;
      gallons=0;
      miles=0;
      total=0;
      average=0;
      counter=0;
      Z=0;
      while(gallons>=0){
         printf("\nEnter the gallons used(-1 to end):");
         scanf("%f",&gallons);
         if(gallons<0){
         printf("\nThe overall average miles/gallon was%f\n",average);
         return 0;}
         printf("Enter the miles driven:");
         scanf("%f",&miles);
         Z=miles/gallons;
         counter=counter+1;
         total=Z+total;
         average=total/counter;
         printf("The miles/gallon for this tank was%f\n",Z);}
         printf("\nThe overall average miles/gallon was%f\n",average);
         return 0;
}


总之都要感谢你们的帮忙
2008-10-18 11:16
快速回复:菜鸟求救
数据加载中...
 
   



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

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