gpt4 book ai didi

c++ - 关于计算用户输入百分比的简单 C++ 程序

转载 作者:搜寻专家 更新时间:2023-10-31 02:16:19 24 4
gpt4 key购买 nike

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int amount;
int discount;
cout<<"please enter amount : ";
cin>>amount;

discount = amount*(10/100);
cout<<"the discount amount is"<<discount<<endl;
system("PAUSE");
}

我正在编写简单的 C++ 程序,我想在其中获得用户输入的输入数字的答案简单的数学方程式在 int amount 变量中计算但它没有给出所需的答案它给出了 0 但我猜代码是正确的.

最佳答案

10/100 是整数除法,结果为 0。因此,您将金额乘以零,再次 Netty 为零。

编辑:

如果您来自 JavaScript 或 Python 等动态语言,它们通常对所有内容都使用隐式双变量,因此这会给您带来预期的值(value)。 C++ 具有更强大的类型系统,因此整除总是会产生另一个整数。如果您想要对浮点值进行除法,则需要使用浮点文字(或强制转换,但在这种情况下没有必要):

discount = amount * 10.0 / 100.0;

或者,如果 float 对您来说足够精确:

discount = amount * 10.0f / 100.0f;

关于c++ - 关于计算用户输入百分比的简单 C++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37045754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com