Úvod do vzorů v C #

Vzory jsou opakovaným dekorativním designem. Existuje jednoduchý kód pro psaní vzorů v C #. Můžeme napsat kód pro tisk různých typů vzorů, jako je hvězdný vzor, ​​znakový vzor a číselný vzor. Níže jsou uvedeny různé příklady tisku vzorů hvězd, znaků a číselných hodnot. Tyto příklady se skládají ze smyček nebo vnořených smyček, což je smyčka uvnitř smyčky. Vzory jsou způsob, jak navrhovat v pořadí nebo logicky. Můžeme tisknout trojúhelníky, pyramidy, diamanty a další symetrie.

Top 3 typy vzorů v C #

Níže jsou uvedeny tři nejlepší vzory v c #.

1. Hvězdný vzor

Následuje příklad tisku vzorů hvězd.

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

2. Číselné vzory

Následuje příklad tisku vzorů čísel.

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

3. Znakový vzor

Níže jsou uvedeny příklady tisku vzorů znaků.

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Příklad č. 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();
)
)
)

Výstup:

Závěr

Tak výše jsou některé příklady různých typů vzorů. Můžeme vytisknout jakýkoli typ vzoru s některými změnami ve smyčkách.

Doporučené články

Toto je průvodce vzory v C #. Zde diskutujeme úvod a tři nejlepší typy vzorů v C # spolu s příklady a implementací kódu. Další informace naleznete také v následujících článcích

  1. Co je návrhový vzor v C #?
  2. C # Design Pattern Interview Otázky
  3. 2D pole v C #
  4. Převažující v C #
  5. Převažující v Javě
  6. 3 různé typy polí v PHP (příklady)
  7. Číselné vzory v Javě s příklady

Kategorie: