Программа рисует квадрат с 3-мя различными методами - C#
Формулировка задачи:
Напишите пожалуйста код:
Использовать 3 метода к добавлению метода Main:
static char GetCharValue(string message)
static int GetIntValue(string message, int min, int max)
static void DrawBox(int sideLength, char character)
GetIntValue выводит сообщение, получает ввод от пользователя и возвращает значение между min и max.
Также выдает ошибку если введены неверные данные.
GetCharValue выводит сообщение и возвращает первый введенный символ
DrawBox рисует квадрат который состоит из введенного символа и длинной из параметра sideLenght
Метод Main будет запрашивать у пользователя длину стороны квадрата и символ, чтобы нарисовать квадрат. Метод DrawBox вызывается что бы нарисовать квадрат.
Так это должно быть в консоле:
Enter the lengthof one side of a square [2 - 20]: 1
Value too small. Min value is 2
Enter the lengthof one side of a square [2 - 20]: 20
Enter a single character: 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
Do you want to draw a new box [y/n]:
Очень надеюсь на помощь.
Спасибо
Решение задачи: «Программа рисует квадрат с 3-мя различными методами»
textual
Листинг программы
static char GetCharValue(string message)
{
Console.Write(message);
return char.Parse(Console.ReadLine().Substring(0, 1));
}
static int GetIntValue(string message, int min, int max)
{
Console.Write(message);
int a;
string b = "";
if (int.TryParse(Console.ReadLine(), out a))
{
if (a >= min && a <= max)
{
return a;
}
else if (a < min)
{
b = "Value is too small. Min value is " + min;
}
else if (a > max)
{
b = "Value is too big. Max value is " + max;
}
}
throw new Exception(b);
}
static void DrawBox(int sideLength, char character)
{
int h = sideLength;
int w = sideLength;
for (int i = 0; i < h; i++)
{
if (i == 0 || i == h - 1)
{
for (int j = 0; j < w; j++)
{
Console.Write(character);
}
Console.Write("\n");
}
else if (i != 0 && i != h - 1)
{
Console.Write(character);
for (int f = 0; f < w - 2; f++)
{
Console.Write(" ");
}
Console.WriteLine(character);
}
}
Console.Write("\n");
}
static void Main(string[] args)
{
while (true)
{
try
{
DrawBox(GetIntValue("Enter the length of one side of a square [2 - 20]: ", 2, 20), GetCharValue("Enter a single character: "));
Console.WriteLine("Do you want to draw a new box? (y/n)");
switch (Console.ReadLine())
{
case "y":
continue;
case "n":
default:
Environment.Exit(0);
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
continue;
}
}
}