gpt4 book ai didi

c++ - C++:尽管我输入的数字为数千,但程序仅读取输入的数字为数百

转载 作者:行者123 更新时间:2023-12-03 07:23:08 25 4
gpt4 key购买 nike

这是我的任务:

Compute for the cash value of a P 25,000 Savings Bond at any time according to the following:

  • If the Bond is less than 6 months old, the interest is zero percent.
  • If the Bond is between 6 and 12 months old, the interest rate is 1 percent.
  • If the Bond is between 13 to 18 months old, the interest rate is 2 percent.
  • If the bond is between 19 to 24 months old, the interest rate is 3.5 percent.

这是我的代码:
#include <iostream>
using namespace std;

int main()
{
int Savings_Bond = 25000;
int Age_Bond = 6 && 12 && 13 && 18 && 19 && 24;
int Interest = 0.01 && 0.02 && 0.035;
int TotalV;

cout << "How long this bond been on? : ";
cin >> Age_Bond;

if (Age_Bond < 6)
{
TotalV = Savings_Bond * 0;
cout << "Your Interest Rate : "
<< "0%"
<< "\n";
cout << "Total Value : " << TotalV << "\n";
}
else if (Age_Bond = 6 + 12)
{
TotalV = Savings_Bond * 0.01;
cout << "Your Interest Rate : "
<< "1%"
<< "\n";
cout << "Total Value : " << TotalV << "\n";
}
else if (Age_Bond = 13 + 18)
{
TotalV = Savings_Bond * 0.02;
cout << "Your Interest Rate : "
<< "2%"
<< "\n";
cout << "Total Value : " << TotalV << "\n";
}
else if (Age_Bond = 19 + 24)
{
TotalV = Savings_Bond * 0.035;
cout << "Your Interest Rate : "
<< "3.5%"
<< "\n";
cout << "Total Value : " << TotalV << "\n";
}
return 0;
}
外观如下:
How long this bond been on? : 17
Your Interest Rate : 1%
Total Value : 250

最佳答案

您似乎对条件表达式在C++中的工作方式有一些误解。

If the Bond is between 6 and 12 months old, the interest rate is 1percent


在C++中这样写
else if (Age_Bond >= 6 && Age_Bond <= 12)
你还有其他问题
int Age_Bond = 6 && 12 && 13 && 18 && 19 && 24;
int Interest = 0.01 && 0.02 && 0.035;
我不确定您认为这些声明在做什么,但这没什么用。您无需使用 Interest变量,因此可以将其删除。而且 Age_Bond变量从用户输入中获取它的值,因此您无需在此处给出值。只需使用
int Age_Bond;
最后,您的值(value)计算不正确。如果利息为0%,则债券的值(value)为25000,但根据您的计算,该值(value)将为零。如果利息为1%,则值为25250,但是您的计算结果为250,依此类推。基本上,您需要将利息添加到现有值中。

关于c++ - C++:尽管我输入的数字为数千,但程序仅读取输入的数字为数百,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64482698/

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