[英]c# recursive function not all code paths return a value
我试图做递归函数,但我得到这个错误: 不是所有的代码路径都返回一个值,我知道为什么我得到这个错误,因为if
没有返回的东西,但我不希望它返回一些东西......如何绕过这个错误? (它应该只是警告)
private double calculate(double money, int months)
{
months--;
if (months != 0)
calculate(profit * 0.3, months);
else
return profit;
}
编辑:当用户点击按钮时我称之为
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();
}
如果我写回报就像你说的那样不会给出我需要的结果
只需在递归分支中添加一个返回:
if (months != 0)
return calculate(profit * 0.3, months);
...
在代码中添加return
值以进行递归:
if (months != 0)
return calculate(profit * 0.3, months);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.