我需要使用 for 循环显示数字 1-10 的平方。这是我到目前为止所拥有的。我不知道我错过了什么。任何帮助将非常感激。
for (int counter = 1; counter <= 10; counter++)
{
Console.WriteLine(counter * counter);
}
Console.ReadLine();
最佳答案
看看你的代码
for (int counter = 1; counter <= 10; counter++)
{
if ((counter * counter) == 0) // this will never evaluate to true
{
Console.WriteLine(counter);
}
}
由于您从 1 开始,您的 if 条件永远不会为真,因此不会打印任何内容
你只需要在 for 循环中使用 counter * counter
或者你可以使用 Math.Pow(counter, 2.0)
来得到你的方 block
关于c# - 求一个数的平方的公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13577958/