使用循环生成C#中的数字及其平方的列表
我想用一个for循环,用C#制作一个数字及其平方的列表 现在我有:使用循环生成C#中的数字及其平方的列表,c#,list,for-loop,perfect-square,C#,List,For Loop,Perfect Square,我想用一个for循环,用C#制作一个数字及其平方的列表 现在我有: namespace ConsoleApplication { class Program { static void Main(string[] args) { int counter; int square = 1; const int maxValue = 10; Consol
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int counter;
int square = 1;
const int maxValue = 10;
Console.WriteLine("Number Square");
Console.WriteLine("-------------------");
{
for (counter = 1; counter <= maxValue; counter++)
square = counter ^ 2;
Console.WriteLine("{0} {1}", counter, square);
}
}
}
}
命名空间控制台应用程序
{
班级计划
{
静态void Main(字符串[]参数)
{
整数计数器;
整数平方=1;
const int maxValue=10;
控制台写入线(“数字广场”);
Console.WriteLine(“------------------------”;
{
对于(counter=1;counter您无意中使用了一个速记来声明for循环块
for语句后面应该跟大括号,以指示要执行的块。但是,如果跳过大括号,它只会抓住“下一行”。在您的情况下,循环中只执行square=counter ^2;
。但是,^运算符用于xor操作,而不是pow
您希望这样做:
Console.WriteLine("Number Square");
Console.WriteLine("-------------------");
for (counter = 1; counter <= maxValue; counter++)
{
square = counter * counter;
Console.WriteLine("{0} {1}", counter, square);
}
Console.WriteLine(“数字方块”);
Console.WriteLine(“------------------------”;
对于(counter=1;counter您无意中使用了一个速记来声明for循环块
for语句后面应该跟大括号,以指示要执行的块。但是,如果跳过大括号,它只会抓住“下一行”。在您的情况下,循环中只执行square=counter ^2;
。但是,^运算符用于xor操作,而不是pow
您希望这样做:
Console.WriteLine("Number Square");
Console.WriteLine("-------------------");
for (counter = 1; counter <= maxValue; counter++)
{
square = counter * counter;
Console.WriteLine("{0} {1}", counter, square);
}
Console.WriteLine(“数字方块”);
Console.WriteLine(“------------------------”;
对于(counter=1;counter请尝试以下计数器循环:
for (counter = 1; counter <= maxValue; counter++)
{
square = Math.Pow(counter, 2);
Console.WriteLine("{0} {1}", counter, square);
}
for(counter=1;counter为您的计数器循环尝试以下操作:
for (counter = 1; counter <= maxValue; counter++)
{
square = Math.Pow(counter, 2);
Console.WriteLine("{0} {1}", counter, square);
}
for(counter=1;counter的^运算符不用于此目的。请改用System.Math.Pow()。例如:
var square=Math.Pow(3,2)
。这将得到9。操作符不是用于此目的。请改用System.Math.Pow()。例如:
var square=Math.Pow(3,2)
。这将得到9。square=counter^2
??
这里的^
是一个
这样做:
square=计数器*计数器;
并附上
{
square = counter * counter;
Console.WriteLine("{0} {1}", counter, square);
}
内部for-loop
或者更好地使用方法square=计数器^2
??
这里的^
是一个
这样做:
square=计数器*计数器;
并附上
{
square = counter * counter;
Console.WriteLine("{0} {1}", counter, square);
}
内部for-loop
或者更好地使用方法支架的放置非常重要:
Console.WriteLine("Number Square");
Console.WriteLine("-------------------");
for (counter = 1; counter <= maxValue; counter++)
{
square = counter * counter;
Console.WriteLine("{0} {1}", counter, square);
}
Console.WriteLine(“数字方块”);
Console.WriteLine(“------------------------”;
对于(counter=1;counter而言,支架的放置非常重要:
Console.WriteLine("Number Square");
Console.WriteLine("-------------------");
for (counter = 1; counter <= maxValue; counter++)
{
square = counter * counter;
Console.WriteLine("{0} {1}", counter, square);
}
Console.WriteLine(“数字方块”);
Console.WriteLine(“------------------------”;
for(counter=1;counterfor循环处于速记模式。您的控制台.writeline
在for循环之外
试着用这个替换线路
for (counter = 1; counter <= maxValue; counter++)
{
square = counter * counter;
Console.WriteLine("{0} {1}", counter, square);
}
for(counter=1;counter您的for循环处于速记模式。您的控制台.writeline
在for循环之外
试着用这个替换线路
for (counter = 1; counter <= maxValue; counter++)
{
square = counter * counter;
Console.WriteLine("{0} {1}", counter, square);
}
对于(计数器=1;计数器我将使用:
private void sqtBtn_Click(object sender, EventArgs e)
{
outputList.Items.Clear();
int itemValue, sqt;
for (int i = 0; i < randomNumAmount; i++)
{
int.TryParse(randomList.Items[i].ToString(), out itemValue);
outputList.Items.Add(Math.Sqrt(itemValue).ToString("f"));
}
}
private void sqtBtn_单击(对象发送方,事件参数e)
{
outputList.Items.Clear();
int itemValue,sqt;
for(int i=0;i
我会使用:
private void sqtBtn_Click(object sender, EventArgs e)
{
outputList.Items.Clear();
int itemValue, sqt;
for (int i = 0; i < randomNumAmount; i++)
{
int.TryParse(randomList.Items[i].ToString(), out itemValue);
outputList.Items.Add(Math.Sqrt(itemValue).ToString("f"));
}
}
private void sqtBtn_单击(对象发送方,事件参数e)
{
outputList.Items.Clear();
int itemValue,sqt;
for(int i=0;i
这就是为什么我喜欢在所有东西上使用花括号!你不需要做任何乘幂运算或乘法运算,因为所有的完美平方都是连续奇数的和,所以你可以通过简单的加法来解决这个问题:这就是为什么我喜欢在所有东西上使用花括号的原因!你不需要做任何乘幂运算或乘法运算,因为所有的完美平方都是奇数的和战神是连续奇数的总和,所以你可以用一个简单的加法来解决这个问题:这解释了那部分。谢谢。那解释了那部分。谢谢。请添加一些文字注释。请添加一些文字注释。