C++ 为什么我的C++;程序在此点之后不工作 #包括 int main() { std::cout>invmoney; 它做它应该做的事 #include <iostream> int main() { std::cout << "Nerdrvox skzbnakan gumar = "; double invmoney = 0; std::cin >> invmoney; std::cout << std::endl << "Nerdrvox skzbnakan gumar = " << invmoney; std::cout << std::endl << "Qani tarov = "; int years = 0; std::cin >> years; std::cout << std::endl << "Qani tarov = " << years; /* * After this point the cmd says press any key to continue, after pressinga key * cmd closes. However it is supposed to do this. */ double summary = invmoney; int months = 12 * years; for (int i = months; i < 0; i--) { std::cout << "Month " << i - months << std::endl << "Invested money" << summary << std::endl; double percent = summary * 0.1; std::cout << "Add percent" << percent << std::endl; summary += percent; std::cout << "Sum for month " << i - months << "is " << summary; } return 0; }

C++ 为什么我的C++;程序在此点之后不工作 #包括 int main() { std::cout>invmoney; 它做它应该做的事 #include <iostream> int main() { std::cout << "Nerdrvox skzbnakan gumar = "; double invmoney = 0; std::cin >> invmoney; std::cout << std::endl << "Nerdrvox skzbnakan gumar = " << invmoney; std::cout << std::endl << "Qani tarov = "; int years = 0; std::cin >> years; std::cout << std::endl << "Qani tarov = " << years; /* * After this point the cmd says press any key to continue, after pressinga key * cmd closes. However it is supposed to do this. */ double summary = invmoney; int months = 12 * years; for (int i = months; i < 0; i--) { std::cout << "Month " << i - months << std::endl << "Invested money" << summary << std::endl; double percent = summary * 0.1; std::cout << "Add percent" << percent << std::endl; summary += percent; std::cout << "Sum for month " << i - months << "is " << summary; } return 0; },c++,C++,它做它应该做的事 #include <iostream> int main() { std::cout << "Nerdrvox skzbnakan gumar = "; double invmoney = 0; std::cin >> invmoney; std::cout << std::endl << "Nerdrvox skzbnakan gumar = " << invmoney;

它做它应该做的事

#include <iostream>

int main()
{
    std::cout << "Nerdrvox skzbnakan gumar = ";
    double invmoney = 0;
    std::cin >> invmoney;
    std::cout << std::endl << "Nerdrvox skzbnakan gumar = " << invmoney;
    std::cout << std::endl << "Qani tarov = ";
    int years = 0;
    std::cin >> years;
    std::cout << std::endl << "Qani tarov = " << years;

    /*
     * After this point the cmd says press any key to continue, after pressinga key
     * cmd closes. However it is supposed to do this.
     */

    double summary = invmoney;

    int months = 12 * years;
    for (int i = months; i < 0; i--)
    {
        std::cout << "Month " << i - months << std::endl << "Invested money"
                << summary << std::endl;
        double percent = summary * 0.1;
        std::cout << "Add percent" << percent << std::endl;
        summary += percent;
        std::cout << "Sum for month " << i - months << "is " << summary;
    }

    return 0;
}

是的,这是因为for循环没有执行

for (int i = months; i>0; i--)

具有以下结构:

for (int i = months; i>0; i--)

只要
条件
为真,for循环将继续执行。在原始示例中,
i
被设置为
months
,这取决于您的输入,将大于或等于零。这将使条件
i为是,因为for循环未执行

for (int i = months; i>0; i--)

具有以下结构:

for (int i = months; i>0; i--)
只要
条件
为真,for循环将继续执行。在您最初的示例中,
i
被设置为
months
,这取决于您的输入,将大于或等于零。这将使条件
i成为您的循环条件:

for (initialization; condition; increase) statement;
for(int i=months;i您的循环条件:

for (initialization; condition; increase) statement;

for(int i=months;i使用调试器并跟踪执行情况。它正在执行您告诉它的操作。您告诉它循环“而
i
小于0”,但由于
i
开始于
months
,这大概是
=0
,因此您的程序从不执行任何操作。请使用调试器并跟踪执行情况。它正在执行您告诉它的操作。您已告诉它“当
i
小于0时”循环,但由于
i
开始于
months
,这大概是
=0
,您的程序从未做过任何事情。您以15秒的速度击败我,或者它必须是相反的:P您以15秒的速度击败我,或者它必须是相反的:P