我正在制作一个家庭作业计划,到目前为止,除了星球"海王星"以外,所有东西都打印得正确。每个其他选择工作正常,并打印出他们应该是的数字,但海王星打印出正确的数字,但作为负面。我被告知要将双重投入很长一段时间,但我相信我已经完成并且没有任何区别。任何帮助或指导将不胜感激。我会附上我的代码,以便你们可以看看它。另一件事是,当海王星被选中时,选择应该是' h'然后,重量是160,速度是595.此外,以小时为单位的旅行时间打印数字是4537815,天数189076,年份是518.02。小时和天数正确出现,除了最后一个数字需要在我的代码中向上舍入一个,并且年份正确,除非它是负数。
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main()
{
char selection;
double weight = 0.0;
double speed = 0.0;
double surfGrav;
double distEarthToSun = 93 * 1000000;
double distfromSun = 0.0;
double newSunDist;
double newWeight;
double distFromPlanets;
// double travelTime;
double travelTimeHours;
double travelTimeDays;
double travelTimeYears;
string planetName;
cout << "Welcome to INTERPLANETARY TRAVEL PROGRAM!\n";
cout << "This program enables you to find out your travel time to the planet\n"
<< "you want to travel to as well as your weight on that planet.\n";
cout << "Please enjoy the program and find the perfect planet for you!\n";
cout << '\n';
cout << '\n';
cout << "INTERPLANETARY TRAVEL MENU\n";
cout << "--------------------------\n";
cout << "a) Mercury\n";
cout << "b) Venus\n";
cout << "c) Earth\n";
cout << "d) Mars\n";
cout << "e) Jupiter\n";
cout << "f) Saturn\n";
cout << "g) Uranus\n";
cout << "h) Neptune\n";
cout << "q) quit\n";
cout << '\n';
cout << "Select a planet to travel to or q to quit the program: \n";
cin >> selection;
if (selection >= 'a' && selection <= 'h')
{
cout << "Please enter your weight (in lbs): \n";
cin >> weight;
cout << "Please enter the speed (in mile per hour) that you would like to travel at: \n";
cin >> speed;
}
if (selection == 'a')
{
planetName = "Mercury";
surfGrav = 0.27;
distfromSun = 36;
}
else if (selection == 'b')
{
planetName = "Venus";
surfGrav = 0.86;
distfromSun = 67;
}
else if (selection == 'c')
{
planetName = "Earth";
surfGrav = 1.00;
distfromSun = 93;
}
else if (selection == 'd')
{
planetName = "Mars";
surfGrav = 0.37;
distfromSun = 141;
}
else if (selection == 'e')
{
planetName = "Jupiter";
surfGrav = 2.64;
distfromSun = 483;
}
else if (selection == 'f')
{
planetName = "Saturn";
surfGrav = 1.17;
distfromSun = 886;
}
else if (selection == 'g')
{
planetName = "Uranus";
surfGrav = 0.92;
distfromSun = 1782;
}
else if (selection == 'h')
{
planetName = "Neptune";
surfGrav = 1.44;
distfromSun = 2793;
}
else if (selection == 'q')
{
cout << "You have chosen to quit the program. Thank you for using the program!\n";
return 0;
}
else
{
cout << "You have entered an invalid selection.\n";
return 0;
}
newWeight = weight * surfGrav;
newSunDist = distfromSun * 1000000;
unsigned long l1 = newSunDist;
if (selection <= 'a' || selection >= 'h')
{
distFromPlanets = distEarthToSun - l1;
}
else
{
distFromPlanets = l1 - distEarthToSun;
}
travelTimeHours = distFromPlanets/speed;
travelTimeDays = travelTimeHours/24.0;
travelTimeYears = travelTimeDays/365.0;
cout <<'\n';
cout << "INTERPLANETARY TRAVEL: Earth to " << planetName << '\n';
cout << "--------------------------------------------------\n";
cout << fixed << setprecision(2) << "Your weight on " << planetName << ": " << newWeight << " lbs\n";
cout << '\n';
cout << "Your travel time to " << planetName << ":\n";
cout << fixed << setprecision(2) << " " << "In Hours: " << (long int)(travelTimeHours + 0.5) << " hours\n";
cout << fixed << setprecision(2) << " " << "In Days : " << (long int)(travelTimeDays + 0.5) << " days\n";
cout << fixed << setprecision(2) << " " << "In Years : " << travelTimeYears << " years\n";
cout << '\n';
return 0;
}
感谢您提前帮助,如果您需要更多信息,请告诉我们。谢谢。
答案 0 :(得分:2)
问题出现在您的变量distFromPlanets
,其解析为-2.7e + 9,distEarthToSun = 9.3e+7
和newSunDist = 2.8e+9
。因此,9.3e+7 - 2.8e+9
大致相等-2.8e+9
。我不是天文学家,但是如果你改变你的if语句来阅读if(selection <= 'a' || selection > 'h')
,海王星的计算将解析为2.8e+9 - 9.3+7 ~ +2.7e+9
。同样,我不是天文学家,所以我不确定这是否是你应该计算的。