Mönster i C # - Topp 3 typer av mönster i C # med exempel

Innehållsförteckning:

Anonim

Introduktion till mönster i C #

Mönster är den upprepade dekorativa designen. Det finns en enkel kod för att skriva mönster i C #. Vi kan skriva kod för att skriva ut olika typer av mönster som stjärnmönster, karaktärsmönster och talmönster. Nedan följer de olika exemplen för att skriva ut mönster för stjärna, tecken och numeriska värden. Dessa exempel består av slingor eller kapslade slingor som är en slinga inuti för en slinga. Mönster är ett sätt att utforma i följd eller på ett logiskt sätt. Vi kan skriva ut trianglar, pyramider, diamanter och andra symmetrier.

Topp 3 typer av mönster i C #

De tre bästa typerna av mönster i c # nämns nedan.

1. Stjärnmönster

Följande är exempel på utskrift av stjärnmönster.

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

2. Antal mönster

Följande är exempel på utskriftsmönster.

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

3. Karaktärsmönster

De nedan följer exempel för att skriva ut teckenmönster.

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Exempel 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();
)
)
)

Produktion:

Slutsats

ovan är några exempel på olika typer av mönster. Vi kan skriva ut alla typer av mönster med några ändringar i öglorna.

Rekommenderade artiklar

Detta är en guide till mönster i C #. Här diskuterar vi introduktionen och topp 3 typer av mönster i C # tillsammans med dess exempel och kodimplementering. Du kan också titta på följande artiklar för att lära dig mer-

  1. Vad är designmönster i C #?
  2. C # Designmönster Intervjufrågor
  3. 2D-matriser i C #
  4. Övergripande i C #
  5. Övergripande i Java
  6. 3 olika typer av matriser i PHP (exempel)
  7. Antal mönster i Java med exempel