c# - 尝试运行本地函数 C# 后没有结果
问题描述
我对 C# 完全陌生,我正在尝试编写一个函数来计算复利后某个金额何时翻倍(以n年为单位)。
现在,当我在 VS Code 中运行它时,我没有收到任何错误,但我也没有得到结果。到目前为止,这是我的代码:
using System;
int NumberOfYearsToDouble(int Amount, int InterestRate) {
int NumberOfYears = 0;
int Percentage;
while (Amount < (Amount*2)) {
Percentage = (Amount/100) * InterestRate;
Amount = Amount + Percentage;
NumberOfYears++;
}
return NumberOfYears;
}
NumberOfYearsToDouble(1000,8);
Console.WriteLine();
解决方案
您正在创建一个无限循环:
while (Amount < (Amount*2))
Amount
将始终小于Amount*2
, (除非Amount
是负整数)。
您应该存储循环Amount*2
之前的值while
,以便它在Amount
更改时保持不变:
int doubleAmount = Amount*2;
while (Amount < doubleAmount)
推荐阅读
- google-chrome - 如何安全地存储 Chrome 扩展程序数据?
- apache-kafka - 无法在 kafka 中列出经纪人 ID
- r - 从R中的列名中间删除部分
- python - 查找与另一个字典中的键、值对匹配的字典的 Pythonic 方法
- javascript - 当我使用 .innerText 时未定义但当我不使用时则不是
- javascript - 如何延迟加载图像?
- c# - 分块上传视频 Azure blob 存储
- tensorflow - 词向量的中心
- javascript - Vue 数据在 setInterval() 方法中更新,但 dom/view 不更新
- datetime - Java 8 Vavr onFailure 连接