首页 > 解决方案 > 为什么需要以复杂的方式进行计算?

问题描述

我期待完全计算的输出清楚地由

只是在做: 但问题是程序给出了6%的 税收,这与我的预期相反。lrooms = 1 和 srooms = 3,结果应该是 116.6 但不是。

cout << "The total cost is: "<<(((lrooms*35)+(srooms*25))*0.06)<<'\n';

通过这样做,输出按预期给出:

cout<<"The total cost is:
"<<((lrooms*35)+(srooms*25))+(((lrooms*35)+(srooms*25))*0.06)<<"$"<<'\n';

编码:

#include <iostream>
using namespace std;
void services();

int main ()
{
    services();
    return 0;
}
void services()
{

        cout << "Enter the Quantity of Large rooms that you want to be cleaned: ";
        double lrooms{};
        cin >> lrooms;
        cout << "Enter the Quantity of Small rooms that you want to be cleaned: ";
        double srooms{};
        cin >> srooms;
      //cout << "The total cost is: "<<(((lrooms35)+(srooms25))*0.06)<<'\n';
        cout << "The total cost is: " << ((lrooms * 35) + (srooms * 25)) + (((lrooms * 35) + (srooms * 25)) * 0.06) << "$" << '\n';
}

标签: c++

解决方案


lrooms = 1 和 srooms = 3,结果应该是 116.6,但是当使用 0.06 和更改为 1.06 时结果不是 6.6,确实解决了问题。解决方案提供者:@MikeCAT

cout << "The total cost is: "<<(((lrooms*35)+(srooms*25))*1.06)<<'\n';

但我仍然不知道为什么它解决了将 0.06 更改为 1.06 的问题


推荐阅读