本文介绍了救命!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嘿伙计们,我在这个编程方面相对较新,而且我在这里停留了 的任务。我正在尝试让下面的程序响应 整数-1来停止并计算总数。确实如此,但它让我在加仑和里程下输入-1 ,我该如何避免这种情况?谢谢。 //以加仑计算汽油里程数 #include< iostream> #include< ; conio.h> 使用std :: cout; 使用std :: cin ;; 使用std ::固定; #include< iomanip> 使用std :: setprecision; //函数main开始程序执行 int main() { int gal; //使用的加仑数 英里; //行驶里程数 int tg; //所有使用的加仑总数 int tm; //所有行驶里程总数 双mpg; //每加仑英里 总计双倍; //所有驱动器的平均值 //初始化阶段 tg = 0; tm = 0; while(gal!= -1){ cout<< \ n输入使用的加仑数(输入-1表示完成):\ n" ;; cin>> gal; cout<< 输入行驶里程:\ n; cin>>里程; tg = tg + gal; tm = tm + miles; //计算里程每加仑 mpg = static_cast<双> (英里)/加仑; cout<< 这次旅行的每加仑是:\ n << setprecision(6)<< fixed<< mpg; } if(gal!= 0) total = static_cast<双> (tm)/ tg; cout<< \ n总体平均值是:\ n" ;; cout<<总计; ;返回0; }Hey guys, i am relatively new at this programming thing and am stuck on anassignment here. I''m trying to get the program below to respond to theinteger -1 to stop and calculate total. It does, but it makes me enter -1under both gallons and miles, how can i avoid this? Thanks.//Figures gas mileage in gallons#include <iostream>#include <conio.h>using std::cout;using std::cin;;using std::fixed;#include <iomanip>using std::setprecision;// function main begins program executionint main(){int gal; //number of gallons usedint miles; //number of miles drivenint tg; //total of all gallons usedint tm; //total of all miles drivendouble mpg; //miles per gallondouble total; //average of all drives//initialization phasetg = 0;tm = 0;while ( gal != -1 ) {cout << "\nEnter the gallons used (Enter -1 to finish) :\n";cin >> gal;cout << "Enter the miles driven:\n";cin >> miles;tg = tg + gal;tm = tm + miles;//calculate miles per gallonmpg = static_cast < double > ( miles ) / gal;cout << "The per gallon for this trip were :\n" << setprecision ( 6 ) <<fixed << mpg;}if ( gal != 0 )total = static_cast < double > ( tm ) / tg;cout << "\nThe overall average was:\n";cout << total;;return 0;}推荐答案 问候, 拉里 - 反垃圾邮件地址,更改每个''X' 'to''。''直接回复。Regards,Larry--Anti-spam address, change each ''X'' to ''.'' to reply directly. - John Carson--John Carson 这篇关于救命!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 17:08