多次打印相同的数字以形成给定大小的矩形

时间:2015-05-12 15:46:45

标签: c# for-loop console

我有学校作业创建控制台程序,使用基本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++)
        {
        }

1 个答案:

答案 0 :(得分:4)

将循环分开,你需要写出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();
}