[英]How to create a hollow square (user parameters) in C# console applications?
I am new to serious programming with a bit of experience in elementary Pascal. 我是初学者,在初级Pascal方面有一点经验。 I am currently trying to figure out how to create a hollow square with user defined parameters in C#.
我目前正在试图弄清楚如何在C#中创建一个带有用户定义参数的空心方块。 I've managed to get 3 out of the 4 sides but I am out of ideas on how to manage the fourth one.
我已经成功地从4个方面中获得了3个但我对如何管理第4个方面的想法不满意。 This is my code so far:
到目前为止这是我的代码:
class Program
{
static void Main(string[] args)
{
int height;
int width;
int counterH = 0;
int counterW2 = 0;
int counterW1 = 0;
Console.WriteLine("Please input the sizes of the square!");
Console.WriteLine("Please input the height of the square.");
height = int.Parse(Console.ReadLine());
Console.WriteLine("Please input the width of the square");
width = int.Parse(Console.ReadLine());
while (counterW1 < width)
{
Console.Write("--");
counterW1++;}
Console.WriteLine();
while (counterH < height)
{
Console.WriteLine("|");
counterH++;
}
while (counterW2 < width)
{
Console.Write("--");
counterW2++;
}
Console.ReadLine();
}
}
I would also be happy if you can suggest an easier/better/more optimised solution if you think mine is bad. 如果你认为我的不好,你会建议一个更简单/更好/更优化的解决方案,我也很高兴。 Thank you very much for your time!
非常感谢您的宝贵时间!
In your while (counterH < height)
loop you should loop through your width-Value and place some spaces. 在你的
while (counterH < height)
循环中,你应该遍历你的width-Value并放置一些空格。 At the End you can place a Pipe | 在末端,您可以放置烟斗| again.
再次。
Use Console.Write()
and not Console.WriteLine()
at this position, because you dont want a LineFeed / CarriageReturn after your first Pipe. 在此位置使用
Console.Write()
而不是Console.WriteLine()
,因为您不希望在第一个Pipe之后使用LineFeed / CarriageReturn。
Example: 例:
while (counterH < height)
{
Console.Write("|");
int counterW3 = 0;
while (counterW3 < width)
{
Console.Write(" ");
counterW3++;
}
Console.Write("|" + System.Environment.NewLine);
counterH++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.