gpt4 book ai didi

c++ - 似乎一切正常,除了添加

转载 作者:行者123 更新时间:2023-11-27 22:48:53 25 4
gpt4 key购买 nike

我有以下代码:

#include <iostream>
#include <string>
using namespace std;

int main()
{
int LoanValue, LoanRate, LoanValue2;
cout << "Loan: ";
cin >> LoanValue;
cout << "Loan Rate: ";
cin >> LoanRate;
LoanValue2 = LoanValue+(LoanValue*(LoanRate/100));
cout << LoanValue << ", " << LoanValue2 << ", " << LoanValue2/12;
}

如果输入是 6000,那么它会输出 6000、6000 和 5000。它只是看不出 LoanValue2 实际执行了加法。不知道有什么问题

编辑:知道了。这样做:

   #include <iostream>
#include <string>
using namespace std;

int main()
{
int LoanValue, LoanRate;
double LoanValue2;
cout << "Loan: ";
cin >> LoanValue;
cout << "Loan Rate: ";
cin >> LoanRate;
LoanValue2 = LoanValue+(LoanValue*(double(LoanRate)/100));
cout << LoanValue << ", " << LoanValue2 << ", " << LoanValue2/12;
}

最佳答案

因为你所有的变量都是整数,所以你在这里使用整数除法:

LoanValue2 = LoanValue+(LoanValue*(LoanRate/100));

例如如果 LoanRate5,则 5/100 == 0。您可以使用 double 变量修复它。

关于c++ - 似乎一切正常,除了添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39830156/

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