Heim >Backend-Entwicklung >C#.Net-Tutorial >Muster in C#
Muster sind das wiederholte dekorative Design. Es gibt einen einfachen Code zum Schreiben von Mustern in C#. Wir können Code schreiben, um verschiedene Arten von Mustern wie Sternmuster, Buchstabenmuster und Zahlenmuster zu drucken. Nachfolgend finden Sie verschiedene Beispiele zum Drucken von Mustern aus Stern-, Zeichen- und Zahlenwerten. Diese Beispiele bestehen aus Schleifen oder verschachtelten Schleifen, bei denen es sich um eine Schleife innerhalb einer Schleife handelt. Muster sind eine Möglichkeit, nacheinander oder logisch zu entwerfen. Wir können Dreiecke, Pyramiden, Rauten und andere Symmetrien drucken.
Die drei wichtigsten Mustertypen in C# sind unten aufgeführt.
Im Folgenden finden Sie Beispiele zum Drucken von Sternmustern.
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Im Folgenden finden Sie Beispiele zum Drucken von Zahlenmustern.
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Im im Folgenden finden Sie Beispiele zum Drucken von Zeichenmustern.
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Ausgabe:
Beispiel #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(); } } }
Output:
Example #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(); } } }
Output:
Example #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(); } } }
Output:
Example #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(); } } }
Output:
Example #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(); } } }
Output:
Example #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(); } } }
Output:
So above are some examples of various types of patterns. We can print any type of pattern with some changes in the loops.
Das obige ist der detaillierte Inhalt vonMuster in C#. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!