Вступ до візерунків у C #
Візерунки - це неодноразовий декоративний дизайн. Існує простий код для написання шаблонів на C #. Ми можемо написати код для друку різних типів візерунків, таких як зірковий малюнок, малюнок символів та цифра. Нижче наведено різні приклади для друку зразків зіркових, символьних та числових значень. Ці приклади складаються з петель або вкладених петель, що представляє собою петлю всередині циклу. Візерунки - це спосіб проектування послідовно або логічно. Ми можемо друкувати трикутники, піраміди, алмази та інші симетрії.
3 найкращі типи візерунків у C #
Нижче згадуються топ-3 типи візерунків у c #.
1. Зоряний візерунок
Нижче наведено приклади друку зіркових візерунків.
Приклад №1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
(
class Program
(
static void Main(string() args)
(
int x, y, z;
for (x =6; x >= 1; x--)
(
for (y = 1; y < x; y++)
(
Console.Write(" ");
)
for (z = 6; z >= x; z--)
(
Console.Write("*");
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 1; x <= 6; x++)
(
for (y = 1; y <= x; y++)
(
Console.Write("*");
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 5; x >= 1; x--)
(
for (y = 1; y <= x; y++)
(
Console.Write("*");
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
(
class Program
(
static void Main(string() args)
(
int x, y, z;
for (x = 5; x >= 1; x--)
(
for (y = 5; y > x; y--)
(
Console.Write(" ");
)
for (z = 1; z <=x; z++)
(
Console.Write("*");
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
(
class Program
(
static void Main(string() args)
(
int x, y, z;
for (x= 1; x <= 5; x++)
(
for (y = x; y < 5; y++)
(
Console.Write(" ");
)
for (z = 1; z < (x * 2); z++)
(
Console.Write("*");
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
(
class Program
(
static void Main(string() args)
(
int x, y, z;
for (x = 5; x >= 1; x--)
(
for (y = 5; y > x; y--)
(
Console.Write(" ");
)
for (z = 1; z < (x * 2); z++)
(
Console.Write("*");
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 1; x <= 5; x++)
(
for (y = x; y < 5; y++)
(
Console.Write(" ");
)
for (y = 1; y <= (2 * x - 1); y++)
(
if (x == 5 || y == 1 || y == (2 * x - 1))
(
Console.Write("*");
)
else
(
Console.Write(" ");
)
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 1; x <= 5; x++)
(
for (y = 1; y <= 5; y++)
(
Console.Write("*");
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 1; x <= 5; x++)
(
for (y = 1; y <= x; y++)
(
if (y == 1 || y== x || x == 5)
(
Console.Write("*");
)
else
(
Console.Write(" ");
)
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
2. Шаблони чисел
Нижче наведено приклади для друку моделей чисел.
Приклад №1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 1; x <= 5; x++)
(
for (y = 1; y <= x; y++)
(
Console.Write(y);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 5; x >= 1; x--)
(
for (y = 1; y <= x; y++)
(
Console.Write(y);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 5; x >= 1; x--)
(
for (y = x; y <= 5; y++)
(
Console.Write(y);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 1; x <= 5; x++)
(
for (y = x; y <= 5; y++)
(
Console.Write(y);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 1; x <= 5; x++)
(
for (y = 1; y <= x; y++)
(
Console.Write(x);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 5; x >= 1; x--)
(
for (y = 5; y >= x; y--)
(
Console.Write(x);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 5; x >= 1; x--)
(
for (y = 1; y <= x; y++)
(
Console.Write(x);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 1; x <= 5; x++)
(
for (y = 5; y >= x; y--)
(
Console.Write(x);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 6; x >= 1; x--)
(
for (y = x; y >= 1; y--)
(
Console.Write(y);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 1; x <= 5; x++)
(
for (y = 6; y >= x; y--)
(
Console.Write(y);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
for (x = 7; x >= 1; x -= 2)
(
for (y = 1; y <= x; y++)
(
Console.Write(y);
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
3. Шаблон персонажа
The нижче - приклади друку шаблонів символів.
Приклад №1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
int z = 5;
for (x = 1; x <= z; x++)
(
for (y = 1; y <= x; y++)
(
Console.Write((char)(x + 64));
)
Console.WriteLine("");
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
int z = 5;
for (x = 1; x <= z; x++)
(
for (y = x; y <= z; y++)
(
Console.Write((char)(x + 64));
)
Console.WriteLine("");
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
int z = 5;
for (x = 1; x <= z; x++)
(
for (y = 1; y <= x; y++)
(
Console.Write((char)(z - x + 1 + 64));
)
Console.WriteLine("");
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
int z = 5;
for (x = 1; x <= z; x++)
(
for (y = x; y<= z; y++)
(
Console.Write((char)(y + 64));
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y, z;
int k = 5;
for (x = 1; x <= k; x++)
(
for (y = 1; y <= k - x; y++)
(
Console.Write(" ");
)
for (z = 1; z <= x; z++)
(
Console.Write((char)(x + 64));
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
int a = 5;
for (x = 1; x <= a; x++)
(
for (y = x; y >= 1; y--)
(
Console.Write((char)(y + 64));
)
Console.WriteLine("");
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №7
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
int a = 5;
for (x = a; x >= 1; x--)
(
for (y = a; y >= x; y--)
(
Console.Write((char)(y + 64));
)
Console.WriteLine("");
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №8
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
int a = 5;
for (x = 1; x <= a; x++)
(
for (y = a; y >= x; y--)
(
Console.Write((char)(y + 64));
)
Console.WriteLine("");
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
int z = 5;
for (x = z; x >= 1; x--)
(
for (y = x; y >= 1; y--)
(
Console.Write((char)(y + 64));
)
Console.WriteLine("");
)
Console.ReadLine();
)
)
)
Вихід:
Приклад №10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CharacterPattern
(
class Program
(
static void Main(string() args)
(
int x, y;
int z = 6;
for (x = 1; x <= z; x++)
(
for (y = 1; y<= z - x; y++)
(
Console.Write(" ");
)
for (y = 1; y <= x; y++)
(
Console.Write((char)(y + 64));
)
for (y = x - 1; y >= 1; y--)
(
Console.Write((char)(y + 64));
)
Console.WriteLine();
)
Console.ReadLine();
)
)
)
Вихід:
Висновок
Так вище наведено кілька прикладів різних типів візерунків. Ми можемо надрукувати будь-який тип візерунка з деякими змінами в петлях.
Рекомендовані статті
Це посібник із шаблонів у C #. Тут ми обговорюємо вступ та найкращі 3 типи шаблонів у C # разом із його прикладами та реалізацією коду. Ви також можете переглянути наступні статті, щоб дізнатися більше -
- Що таке шаблон дизайну в C #?
- C # Питання щодо інтерв'ю дизайну
- 2D масиви в C #
- Переоцінка в C #
- Перевагу в Java
- 3 різні типи масивів у PHP (приклади)
- Числові візерунки в Java з прикладами