下面的代码无法正常工作,如果条件直接进入了else ..如果我将基值设为2并将幂值设为3,则结果显示为1 .....如何更改此值才能正常工作....
class Program
{
static void Main(string[] args)
{
int i, a, result=1;
int b;
Console.WriteLine("enter the base value");
a=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter the power value");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("the vale of {0} to the power {1} is : {2}", a, b, result);
if(b>0)
{
for(i=1; i<=b; i++)
{
result=result*a;
}
}
else if (b<0)
{
for (i = 1; i <= b; i++)
{
result = 1 / (result * a);
}
}
else
{
result = 1;
}
Console.ReadLine();
}
答案 0 :(得分:3)
也许您可能想要移动打印结果的代码,以便在计算之后 ?:
if(b>0)
{
for(i=1; i<=b; i++)
{
result=result*a;
}
}
else if (b<0)
{
for (i = 1; i <= b; i++)
{
result = 1 / (result * a);
}
}
else
{
result = 1;
}
// happens after the calculation
Console.WriteLine("the vale of {0} to the power {1} is : {2}", a, b, result);
请花一些时间来学习如何使用a debugger调试代码。