关于while语句和do while语句的问题
悠隼 2019-06-14 05:22:02 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”赋值。
}
}
}
/////////////////////////////////////////////////////////////////////