我正在编写此代码,在其中计算一个人从地球到达特定星球所花费的时间。我的问题是,对于时间变量,我一直保持为零。结果,其他变量小时和年也给出零。怎么了?
我知道这不是强制转换,因为它们已经加倍了。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
char choice = 0;
double weight = 0;
double speed = 0;
double time = 0;
double newWeight;
double distance = 0;
double gravity = 0;
double newtoSun = 0;
double earthtoSun = 93;
double days = 0;
double years = 0;
string planet;
cout << "welcome" << endl;
cout << "menu" << endl;
cout << "a) mercury" << endl;
cout << "b)Venus" << endl;
cin >> choice;
if (choice >= 'a' && choice <= 'd')
{
cout << "enter weight" << endl;
cin >> weight;
cout << "What speed u want?" << endl;
cin >> speed;
if (choice == 'a')
{
newtoSun = 36;
gravity = 0.27;
planet = "Mercury";
}
else if (choice == 'b')
{
newtoSun = 67;
gravity = 0.86;
planet = "Venus";
}
else if (choice == 'c')
{
newtoSun = 93;
gravity = 1;
planet = "Earth";
}
else if (choice == 'd')
{
newtoSun = 141;
gravity = 0.37;
planet = "Mars";
}
newWeight = weight * gravity;
time = distance / speed;
distance = abs(earthtoSun - newtoSun);
days = time / 24;
years = days / 365;
cout << "INTERPLANETARY TRAVEL: Earth to " << choice << endl;
cout << "-------------------------------------------" << endl;
cout << "Your weight on " << planet << fixed << setw(5)
<< setprecision(2) << newWeight << endl;
cout << distance << endl;
cout << speed << endl;
cout << "Your travel time to " << planet << ":" << endl;
cout << "In Hours: " << setw(2) << time << "hours" << endl;
cout << "In Days : " << setw(2) << days << "days" << endl;
cout << "In Years : " << setw(2) << years << "years" << endl << endl;
}
else if (choice == 'q')
{
cout << "You quit" << endl;
}
else
cout << "You have entered an invalid selection" << endl;
return 0;
}
time = distance / speed;
distance = abs(earthtoSun - newtoSun);
这些语句是向后的。