关于while语句和do while语句的问题

悠隼 悠隼 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
C#新人求解答:以下两个程式写法有什么区别?为什么第二个会提示错误:控制离开当前方法之前必须对 out 参数“interest”赋值。
我无法理解为什么第二个会错,不是也返回了interest值了吗?求高手解释。

第一个
/////////////////////////////////////
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
decimal x = 1000;
decimal y;
decimal z = Gain(x, 3, out y);
Console.WriteLine("本息合计:{0},最后一年利息:{1}", z, y);
Console.ReadLine();

}
public static decimal Gain(decimal x, int n, out decimal interest)
{
int i = 0;
do
{
interest = x * 0.08M;
x += interest;
i++;
}
while (i < n);
return x;
}
}
}
////////////////////////////////////////

第二个
////////////////////////////////////////
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
decimal x = 1000;
decimal y;
decimal z = Gain(x, 3, out y);
Console.WriteLine("本息合计:{0},最后一年利息:{1}", z, y);
Console.ReadLine();

}
public static decimal Gain(decimal x, int n, out decimal interest)
{
int i = 0;
while (i < n)
{
interest = x * 0.08M;
x += interest;
i++;
}

return x; //这里提示我错误:控制离开当前方法之前必须对 out 参数“interest”赋值。
}
}
}
/////////////////////////////////////////////////////////////////////
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
悠隼
C# 111,128 成员 642,536 内容