a12*_*773 5 c# recursion function
我试图做递归函数,但我得到这个错误:不是所有的代码路径都返回一个值我知道为什么我得到这个错误,因为if没有返回的东西,但我不希望它返回一些东西......如何绕过这个错误?(它应该只是警告)
private double calculate(double money, int months)
{
months--;
if (months != 0)
calculate(profit * 0.3, months);
else
return profit;
}
Run Code Online (Sandbox Code Playgroud)
编辑:当用户点击按钮时我称之为
private void bCalculate_Click(object sender, EventArgs e)
{
profit = double.Parse(tbMoney.Text);
months = int.Parse(tbMonth.Text);
tbPpofit.Text = calculate(profit,months+1).ToString();
}
Run Code Online (Sandbox Code Playgroud)
如果我写回报就像你说的那样不会给出我需要的结果
Hen*_*man 11
只需在递归分支中添加一个返回:
if (months != 0)
return calculate(profit * 0.3, months);
...
Run Code Online (Sandbox Code Playgroud)
return在代码中添加一个值以进行递归:
if (months != 0)
return calculate(profit * 0.3, months);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2588 次 |
| 最近记录: |