求一个数的平方的公式

Vin*_*nti 4 .net c# for-loop perfect-square

我需要使用 for 循环显示数字 1-10 的平方。这是我到目前为止所拥有的。我不知道我错过了什么。任何帮助将非常感激。

        for (int counter = 1; counter <= 10; counter++)
        {                          
            Console.WriteLine(counter * counter);                
        }
        Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

V4V*_*tta 5

看看你的代码

for (int counter = 1; counter <= 10; counter++)
{
   if ((counter * counter) == 0) // this will never evaluate to true
   {
       Console.WriteLine(counter);
   }
}
Run Code Online (Sandbox Code Playgroud)

由于您从 1 开始,您的 if 条件永远不会为真,因此不会打印任何内容

你只需要counter * counter在 for 循环中使用 print

或者你可以用它Math.Pow(counter, 2.0)来得到你的方块

  • +1 [`Math.Pow`](http://msdn.microsoft.com/en-us/library/system.math.pow.aspx) (2认同)