0
我想用對角線繪製空心矩形。矩形的高度應大於5,最多20 witdh也應該是大於5,最多80我已經得到這個代碼:如何在控制檯中用對角線繪製空心矩形
using System;
public class Test
{
public static void Main()
{
Console.Write("Enter width of rectangle: ");
int w = int.Parse(Console.ReadLine());
Console.Write("Enter height of rectangle: ");
int h = int.Parse(Console.ReadLine());
Console.WriteLine();
if(w > 5 && w <= 80 && h > 5 && h <= 20)
{
draw(w, h);
}
else
{
Console.WriteLine("Invalid entry!");
}
}
static void draw(int w, int h)
{
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
if ((i == 0) || (j == 0) || (i == h - 1) || (j == w - 1) || (i == j) || (i + j == w - 1))
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
} Console.WriteLine();
}
}
}
但這個代碼繪製正確只爲正方形。對於矩形,它不會正確繪製對角線。任何幫助將不勝感激。
它似乎工作,謝謝。 – fsacer
您是否願意爲初學者級別的程序員解釋更深入的解決方案?對我來說,IsDiagonalHigh和IsDiagonalWide的工作原理可能更具圖形性,因爲我必須向教授解釋它。這個解決方案的工作方式是每次將矩形分成4個相等的部分,並確定繪製恆星的座標? – fsacer