c++ - 为什么需要以复杂的方式进行计算?
问题描述
我期待完全计算的输出清楚地由
只是在做: 但问题是程序给出了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';
}
解决方案
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 的问题
推荐阅读
- flutter - 在颤振中创建可扩展的随机路径
- swift - swift - 执行两个函数,甚至第一个在 or 语句中返回 true
- java - 在 Java 中使用条件(三元)运算符引发异常
- jenkins - Jenkins 工作迁移到 Jenkinsfile
- git - 是否可以重新安排存储库并保留重新分配的文件的提交历史记录?
- php - If-else 使用 PHP,变量未按预期工作
- r - R中值的标签
- python - 如何使用 Python Arcade 有效地调整鼠标坐标?
- django - Docker自动重新加载在远程服务器上不起作用
- python - 从 python 列表中删除任何重复项