Johdatus malleihin C #
Kuviot ovat toistuva koristeellinen muotoilu. C #: n mallien kirjoittamiseen on yksinkertainen koodi. Voimme kirjoittaa koodin tulostaaksesi erityyppisiä kuvioita, kuten tähtikuviota, merkkikuviota ja numerokuviota. Alla on erilaisia esimerkkejä tähti-, merkki- ja numeeristen arvojen tulostamiseen. Nämä esimerkit koostuvat silmukoista tai sisäkkäisistä silmukoista, mikä on silmukka silmukan sisällä. Kuviot ovat tapa suunnitella peräkkäin tai loogisesti. Voimme tulostaa kolmioita, pyramideja, timantteja ja muita symmetrioita.
C #: n 3 suosituinta mallia
Jäljempänä mainitaan c # 3: n suosituimmat mallityypit.
1. Tähtikuvio
Seuraavassa on esimerkkejä tähtikuvioiden tulostamisesta.
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki # 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
2. Numerokuviot
Seuraavat esimerkit tulostavat numerokuviot.
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki # 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki # 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();
)
)
)
lähtö:
Esimerkki # 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();
)
)
)
lähtö:
3. Merkkikuvio
Seuraavassa on esimerkkejä merkkikuvioiden tulostamisesta.
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki # 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki 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();
)
)
)
lähtö:
Esimerkki # 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();
)
)
)
lähtö:
johtopäätös
Niin yllä on joitain esimerkkejä erityyppisistä malleista. Voimme tulostaa minkä tahansa tyyppisiä kuvioita, joissa silmukoissa on joitain muutoksia.
Suositellut artikkelit
Tämä on opas C #: n malleihin. Tässä keskustellaan C #: n johdannosta ja kolmesta suositusta mallilajista sekä sen esimerkeistä ja koodin toteutuksesta. Voit myös katsoa seuraavia artikkeleita saadaksesi lisätietoja-
- Mikä on suunnittelukuvio C #: ssä?
- C # Design Pattern -haastattelukysymykset
- 2D-ryhmät C #: ssä
- Ohittaminen C #: ssä
- Ylivoimainen Java
- 3 erityyppistä taulukkoa PHP: ssä (esimerkit)
- Numerokuviot Java-sovelluksissa esimerkkien avulla