有人能告诉我一个简单的方法来在C#中实现空心矩形吗?如何在控制台中显示一个简单的空心星号矩形?
我已经能够做出一个简单的矩形,但我看过的空心矩形程序包含或数组或非常复杂。例如,another forum上的解决方案似乎太具有挑战性,而this answer on CodeReview.SE太难理解。
这就是我所做的,它显示了一个简单的(填充的)矩形。如果可能,如何使用if
逻辑输出空心矩形?
class Nested_Loops_Hollow_Rectangles
{
public void RunExercise()
{
// how are now supposed to make this hollow?
// columns are side by side, rows is number of top to bottom
// see tut
Console.WriteLine("Welcome to the HollowRectanglePrinter Program.");
Console.WriteLine("How many columns wide should the rectangle be?"); //i.e. 4
int iColMax, iRowMax;
string userChoiceC = Console.ReadLine();
Int32.TryParse(userChoiceC, out iColMax);
Console.WriteLine("How many rows tall should the rectangle be? "); //i.e. 4
string userChoiceR = Console.ReadLine();
Int32.TryParse(userChoiceR, out iRowMax);
Console.WriteLine("Here you go:");
if (iRowMax > 0 || iColMax > 0)
{
for (int iRow = 0; iRow < iRowMax; iRow++)
{
for (int iCol = 0; iCol < iColMax; iCol++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}
我意识到部分答案时,我瞥了一眼你的代码。这写得非常好。 – cinnamon 2015-02-09 00:33:41