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学习者快速成长!