c++ - 为什么它给出0答案?
问题描述
#include <iostream>
using namespace std;
int main()
{
double basic = 1000;
double DA = (10/100) * basic;
double HRA = (30/100) * basic;
cout<<"DA = "<<DA<<endl; // why the answer is 0 ?
cout<<"HRA = "<<HRA<<endl; // why the answer is 0 ?
return 0;
}
请帮助我....我不明白为什么 DA 和 HRA 给出的答案是 0?
解决方案
10/100 = 0 因为 10 和 100 都是 int 并且它们的结果也将是 int。
尝试 10/100.0
30/100.0
推荐阅读
- java - Call a Scala "val function" from Java gives error
- wordpress - 拥有所有相关的 opengraph 社交元(由 AIOS 创建),但 facebook 调试无法识别
- gcloud - 谷歌云构建:设备上没有剩余空间
- python - 使用 numpy 的逐元素矩阵乘法(Hadamard 乘积)
- javascript - 为什么 Object 原型中没有与 Object 相关的方法?
- javascript - Looping Through An Array, Putting Each Index On A New Line, And Displaying End Result In An Alert
- php - Join three table to get some problem help me?
- c# - C# 代码执行停止,没有任何异常
- java - ERROR: Could not find org.jetbrains.kotlin:kotlin-stdlib-jdk8
- ansible - Ansible 循环项怎么可能被循环任务之外的变量引用?