嵌套if else语句C ++

时间:2014-03-15 21:09:11

标签: c++

虽然我有一个语句,如果用户输入q或输入无效语句将退出程序,if和if else语句后面的代码仍会显示。例如,如果用户输入q,代码将编译并读取:"您已选择退出程序。感谢您使用该程序!"但是它也会将重量和旅行时间显示为0。

int main()
{
    string planet;
    char selection;
    double weightEarth = 0;
    double rate = 0;
    double distanceFromSun = 0;
    double surfaceGravity = 0;
    double newWeight = 0;
    double travelTime = 0;
    double travelDistance = 0;
    double distanceFromPlanets = 0;
    double distanceFromEarthToSun = 93.0;

    cout << "INTERPLANETARY TRAVEL MENU\n"
        << "----------------------------\n"
        << "a) Mercury\n"
        << "b) Venus\n"
        << "c) Earth\n"
        << "d) Mars\n"
        << "e) Jupiter\n"
        << "f) Saturn\n"
        << "g) Uranus\n"
        << "h) Neptune\n"
        << "q) Quit\n"
        << "Select a planet to travel to or q to quit the program :\n";

    cin >> selection;

    if (selection <= 'h')
    {

        cout << "Please enter your weight: \n";
        cin >> weightEarth;

        cout << "At what speed would you like to travel at?\n";
        cin >> rate;


        if (selection == 'a')
        {
            planet = "Mercury";
            surfaceGravity = 0.27;
            distanceFromSun = 36;
        }

        else if (selection == 'b')
        {
            planet = "Venus";
            surfaceGravity = 0.86;
            distanceFromSun = 67.0;
        }

        else if (selection == 'c')
        {
            planet = "Earth";
            surfaceGravity = 1.00;
            distanceFromSun = 93.0;
        }

        else if (selection == 'd')
        {
            planet = "Mars";
            surfaceGravity = 0.37;
            distanceFromSun = 141.0;
        }

        else if (selection == 'e')
        {
            planet = "Jupiter";
            surfaceGravity = 2.64;
            distanceFromSun = 483.0;
        }

        else if (selection == 'f')
        {
            planet = "Saturn";
            surfaceGravity = 1.17;
            distanceFromSun = 886.0;
        }

        else if (selection == 'g')
        {
            planet = "Uranus";
            surfaceGravity = 0.92;
            distanceFromSun = 1782.0;
        }

        else
        {
            planet = "Neptune";
            surfaceGravity = 1.44;
            distanceFromSun = 2793.0;
        }

    }

    else if (selection == 'q')
    {
        cout << "You have chosen to quit the program. Thank you for using the program!\n";
    }

    else
    {
        cout << "You have entered an invalid selection.\n";
    }

    newWeight = weightEarth * surfaceGravity;

    distanceFromPlanets = abs(distanceFromSun - distanceFromEarthToSun);

    travelTime = travelDistance / rate;

    cout << "Your weight on " << planet << " :  " << newWeight << endl;
    cout << "Your travel time to " << planet << ":\n";

3 个答案:

答案 0 :(得分:1)

您的qinvalid选择路径仅打印到控制台,它们不会退出程序。您需要每个return 0;

但更好的方法是使用设置的布尔值,如果要退出或选择无效。

这是一种方法。

将布尔值声明为真

bool validPlanet = true;

quitinvalid路径中添加此声明

validPlanet = false;

然后将您的体重计算包裹在if条件下,如此

if( validPlanet)
{
    newWeight = weightEarth * surfaceGravity;

    distanceFromPlanets = abs(distanceFromSun - distanceFromEarthToSun);

    travelTime = travelDistance / rate;

    cout << "Your weight on " << planet << " :  " << newWeight << endl;
    cout << "Your travel time to " << planet << ":\n";

}

我假设您之后立即有return 0;,否则您需要使用validPlanet来确保这些语句被恰当地调用。

答案 1 :(得分:0)

一种简单的方法是将getch()放在语句后跟exit(0);

如果部分成为最后的其他部分:

  else if (selection == 'q')
    {
        cout << "You have chosen to quit the program. Thank you for using the program!\n";
        getch();  //stops the current screen until a key stroke
        exit(0);  //exits the program. (zero value in the argument tells that it was a safe exit)
    }

或者,如果您不希望用户在退出前给出击键,您可以使用delay(time)功能停止屏幕特定时间。

然后代码变为:

  else if (selection == 'q')
    {
        cout << "You have chosen to quit the program. Thank you for using the program!\n";
        delay(1000)  //stops the current screen for 1 second. (time in miliseconds, 1000ms = 1 second)
        exit(0);  //exits the program. (zero value in the argument tells that it was a safe exit)
    }

答案 2 :(得分:0)

您的代码根据您的需要重新格式化 (因为我们还没有在if语句中使用布尔值。目前我还在尝试重新使用构造我的if和if else语句来解决问题,但我似乎无法得到正确的组合。)

    double weightEarth = 0; //made these variables global so that display_result()can access it.
    double rate = 0;
    double distanceFromSun = 0;
    double surfaceGravity = 0;
    double newWeight = 0;
    double travelTime = 0;
    double travelDistance = 0;
    double distanceFromPlanets = 0;
    double distanceFromEarthToSun = 93.0;

    void display_result()
    {
           newWeight = weightEarth * surfaceGravity;

           distanceFromPlanets = abs(distanceFromSun - distanceFromEarthToSun);

           travelTime = travelDistance / rate;

           cout << "Your weight on " << planet << " :  " << newWeight << endl;
           cout << "Your travel time to " << planet << ":\n";
    }

    int main()
    {
    string planet;
    char selection;

    cout << "INTERPLANETARY TRAVEL MENU\n"
        << "----------------------------\n"
        << "a) Mercury\n"
        << "b) Venus\n"
        << "c) Earth\n"
        << "d) Mars\n"
        << "e) Jupiter\n"
        << "f) Saturn\n"
        << "g) Uranus\n"
        << "h) Neptune\n"
        << "q) Quit\n"
        << "Select a planet to travel to or q to quit the program :\n";

    cin >> selection;

    if (selection <= 'h')
    {

        cout << "Please enter your weight: \n";
        cin >> weightEarth;

        cout << "At what speed would you like to travel at?\n";
        cin >> rate;


        if (selection == 'a')
        {
            planet = "Mercury";
            surfaceGravity = 0.27;
            distanceFromSun = 36;
            display_result();
        }

        else if (selection == 'b')
        {
            planet = "Venus";
            surfaceGravity = 0.86;
            distanceFromSun = 67.0;

            display_result();
        }

        else if (selection == 'c')
        {
            planet = "Earth";
            surfaceGravity = 1.00;
            distanceFromSun = 93.0;

            display_result();
        }

        else if (selection == 'd')
        {
            planet = "Mars";
            surfaceGravity = 0.37;
            distanceFromSun = 141.0;
            display_result();
        }

        else if (selection == 'e')
        {
            planet = "Jupiter";
            surfaceGravity = 2.64;
            distanceFromSun = 483.0;
            display_result();
        }

        else if (selection == 'f')
        {
            planet = "Saturn";
            surfaceGravity = 1.17;
            distanceFromSun = 886.0;
            display_result();
        }

        else if (selection == 'g')
        {
            planet = "Uranus";
            surfaceGravity = 0.92;
            distanceFromSun = 1782.0;
            display_result();
        }

        else
        {
            planet = "Neptune";
            surfaceGravity = 1.44;
            distanceFromSun = 2793.0;
            display_result();
        }

    }

    else if (selection == 'q')
    {
        cout << "You have chosen to quit the program. Thank you for using the program!\n";
    }

    else
    {
        cout << "You have entered an invalid selection.\n";
    }
}

我创建了一个函数display_result()并移动了在退出if-else语句时运行的代码。现在只有在您尝试计算重量时才会调用此项,而不是在您退出程序时调用。这解决了程序复杂性和程序冗长的问题。