C# 中的模式

WBOY
發布: 2024-09-03 15:33:39
原創
847 人瀏覽過

圖案是重複的裝飾設計。有一段簡單的程式碼可以用 C# 寫模式。我們可以編寫程式碼來列印不同類型的圖案,如星形圖案、字元圖案和數字圖案。以下是列印星號、字元和數值模式的各種範例。這些範例由循環或巢狀循環組成,巢狀循環是循環內部的循環。模式是一種按順序或邏輯方式進行設計的方式。我們可以列印三角形、金字塔、菱形和其他對稱形狀。

C# 中最常見的 3 種模式

C# 中最常見的 3 種模式如下所述。

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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例#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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例#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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例#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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例#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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例#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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例#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();
}
}
}
登入後複製

輸出:

C# 中的模式

3.字元模式

以下 以下是列印字元模式的範例。

範例#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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例#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();
}
}
}
登入後複製

輸出:

C# 中的模式

範例 #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:

C# 中的模式

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:

C# 中的模式

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:

C# 中的模式

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:

C# 中的模式

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:

C# 中的模式

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:

C# 中的模式

Conclusion

So above are some examples of various types of patterns. We can print any type of pattern with some changes in the loops.

以上是C# 中的模式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!