多次打印相同的数字以形成给定大小的矩形
Print same number multiple times to form rectangle of given size
我的学校作业是创建控制台程序,该程序使用基本 for
循环根据用户的输入创建一个数字字段(矩形)。意思是,用户将写下应该使用哪个数字来填充字段,以及字段应该有多高和多宽。
代码如下:
Console.WriteLine("Hey! Which Number do you want to use to fill the field?");
int fieldNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Okay, how big should be the lenght?");
int fieldSizeX = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Allright, how big should the height be?");
int fieldSizeY = Convert.ToInt32(Console.ReadLine());
Console.Clear();
for (int i = 0, j = 0; i < fieldSizeX && j < fieldSizeY; i++, j++)
{
}
拆分循环,你需要写出X*Y个元素。
//For each row (y)
for (int y = 0; y < fieldSizeY; y++)
{
//For each column (x)
for (int x = 0; x < fieldSizeX; x++)
{
//Now you need to repeat the same number for each x, but no new line.
Console.Write(fieldNumber)
}
//Stick the new line on the end of the row to start the next row
Console.WriteLine();
}
我的学校作业是创建控制台程序,该程序使用基本 for
循环根据用户的输入创建一个数字字段(矩形)。意思是,用户将写下应该使用哪个数字来填充字段,以及字段应该有多高和多宽。
代码如下:
Console.WriteLine("Hey! Which Number do you want to use to fill the field?");
int fieldNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Okay, how big should be the lenght?");
int fieldSizeX = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Allright, how big should the height be?");
int fieldSizeY = Convert.ToInt32(Console.ReadLine());
Console.Clear();
for (int i = 0, j = 0; i < fieldSizeX && j < fieldSizeY; i++, j++)
{
}
拆分循环,你需要写出X*Y个元素。
//For each row (y)
for (int y = 0; y < fieldSizeY; y++)
{
//For each column (x)
for (int x = 0; x < fieldSizeX; x++)
{
//Now you need to repeat the same number for each x, but no new line.
Console.Write(fieldNumber)
}
//Stick the new line on the end of the row to start the next row
Console.WriteLine();
}