一、控制台程序:
亮点:交互性强、检测行数有效性、可重新开始;
不足:还可添加输出形状为直接或等腰的选项;但考虑到控制台程序的用户选择不太便捷,此功能在窗体程序中可以更简洁、优雅的实现,故控制台中并未添加。
①代码如下:
public class ArrayYhsj
{
public void Yhsj(int rows)
{
long[,] yhsj = new long[rows, rows];
// 初始化第一列和对角线
for (int i = 0; i < rows; i++)
{
yhsj[i, 0] = 1;
yhsj[i, i] = 1;
}
// 填充其余的元素
for (int i = 2; i < rows; i++)
{
for (int j = 1; j < i; j++)
{
yhsj[i, j] = yhsj[i - 1, j - 1] + yhsj[i - 1, j];
}
}
// 输出杨辉三角
for (int i = 0; i < rows; i++)
{
for (int j = 0; j <= i; j++)
{
Console.Write(yhsj[i, j] + " ");
}
Console.WriteLine();
}
}
}
internal class Program
{
static void Main(string[] args)
{
ArrayYhsj Yhsj = new ArrayYhsj();
Start: Console.WriteLine("请输入您想要生成杨辉三角的行数:");
for (; ; )
{
if (!int.TryParse(Console.ReadLine(), out int nRows) || nRows <= 0)
Console.WriteLine("输入的行数无效,请重新输入!");
else
{
Console.WriteLine("————{0}行的杨辉三角如下————", nRows);
Yhsj.Yhsj(nRows);
Console.WriteLine("还想继续吗?继续生成请按\"1\",结束请按其他键:");
if (Console.ReadLine() == "1") goto Start;
Environment.Exit(0);
}
}
}
}
②运行结果如下:
二、Windows窗体程序
亮点:交互性强、检测行数有效性、可选择输出的形状;
不足:(1)囿于笔者水平,无法确保输出的结果每次都在生成区中央;(2)曾尝试将生成并输出杨辉三角的代码放在新建类文件里并在Form1.cs里调用,但不知为何总报错或无法正常调用,遂放弃。
①实现功能的代码文件(Form1.cs):
public partial class YangHui : Form
{
public YangHui()
{
InitializeComponent();
}
public string Yhsj(int rows, bool isIsosceles)
{
//创建一个二维数组yhsj,用于存储杨辉三角的值。
int[,] yhsj = new int[rows, rows];
//使用new关键字创建了一个 StringBuilder 类的新实例
/* StringBuilder 类是在内部维护一个可变的字符数组,并在需要时
扩展该数组,因而在连接大量字符串时比使用“+”操作符更高效。*/
StringBuilder result = new StringBuilder();
// 第一个 for 循环:初始化第一列和对角线元素为1
for (int i = 0; i < rows; i++)
{
yhsj[i, 0] = 1;
if (i != 0) yhsj[i, i] = 1;
}
// 第二个 for 循环:填充杨辉三角的其余元素
for (int i = 2; i < rows; i++)
{
for (int j = 1; j < i; j++)
{
//从第二行开始,每个元素的值是其上方和左上方元素之和
yhsj[i, j] = yhsj[i - 1, j - 1] + yhsj[i - 1, j];
}
}
/* 第三个 for 循环:
* (1)外部循环(由变量i控制)遍历杨辉三角的每一行,从第0行开始直到第rows-1行。
* (2)如果isIsosceles为true(意味着用户想要一个等腰的杨辉三角),则内部循环(由变量j控制)会运行,在每一行前
添加数量是rows - i - 1的空格。随着行数i的增加,每行前面的空格数会逐渐减少,从而形成等腰的效果。
* (3)在每个行的开头(无论是否为等腰),另一个内部循环(同样由变量j控制,但此时j的值从0遍历到i)遍历该行的每个元素,
并使用 StringBuilder 类的 Append 方法将它们添加到结果字符串中。每个数字后面都添加了一个空格(" ")以保持输出的整洁。
* (4)每一行处理完后,使用 StringBuilder 类的 AppendLine 方法添加一个换行符,以便下一行从新的一行开始。
* (5)当所有行都处理完后,使用StringBuilder的ToString方法将构建的字符串转换为普通的字符串,并返回该字符串。 */
for (int i = 0; i < rows; i++)
{
if (isIsosceles)
{
for (int j = 0; j < rows - i - 1; j++)
{
result.Append(" ");
}
}
for (int j = 0; j <= i; j++)
{
result.Append(yhsj[i, j] + " ");
}
result.AppendLine();
}
return result.ToString();
}
private void btnForm_Click(object sender, EventArgs e)
{
//使用异常处理来确保用户输入的行数是有效的。如果输入无效,捕获异常并显示一个消息框,然后清除文本框的内容;
//如果没有异常,则调用 Yhsj 方法,并将结果设置为标签 lblResult 的文本。
try
{
int Rows = int.Parse(txtRows.Text);
if (Rows <= 0) throw new Exception("输入的行数不是正整数!");
else if (Rows > 10) throw new Exception("输入的行数太多,可能会超出可见区域!");
else lblResult.Text = Yhsj(Rows, Isosceles.Checked);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
txtRows.Clear();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//清除用户输入的行数和生成的结果,将界面初始化。
txtRows.Clear();
lblResult.Text = "";
Isosceles.Checked = false;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
②窗体程序的入口和初始化代码(Program.cs和Form1.Designer.cs):
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new YangHui());
}
}
partial class YangHui
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnForm = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.lblTitle = new System.Windows.Forms.Label();
this.txtRows = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.lblResult = new System.Windows.Forms.Label();
this.Isosceles = new System.Windows.Forms.CheckBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// btnForm
//
this.btnForm.Font = new System.Drawing.Font("华文中宋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnForm.Location = new System.Drawing.Point(80, 487);
this.btnForm.Name = "btnForm";
this.btnForm.Size = new System.Drawing.Size(106, 39);
this.btnForm.TabIndex = 0;
this.btnForm.Text = "生成";
this.btnForm.UseVisualStyleBackColor = true;
this.btnForm.Click += new System.EventHandler(this.btnForm_Click);
//
// btnClear
//
this.btnClear.Font = new System.Drawing.Font("华文中宋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnClear.Location = new System.Drawing.Point(339, 487);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(106, 39);
this.btnClear.TabIndex = 1;
this.btnClear.Text = "重置";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// btnClose
//
this.btnClose.Font = new System.Drawing.Font("华文中宋", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnClose.Location = new System.Drawing.Point(599, 487);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(106, 39);
this.btnClose.TabIndex = 2;
this.btnClose.Text = "退出";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// lblTitle
//
this.lblTitle.AutoSize = true;
this.lblTitle.Font = new System.Drawing.Font("楷体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblTitle.Location = new System.Drawing.Point(61, 34);
this.lblTitle.Name = "lblTitle";
this.lblTitle.Size = new System.Drawing.Size(487, 72);
this.lblTitle.TabIndex = 4;
this.lblTitle.Text = " 请输入您想要生成的杨辉三角的行数:\r\n\r\n(PS:数字超过10可能导致无法完整显示)";
//
// txtRows
//
this.txtRows.BackColor = System.Drawing.Color.LightYellow;
this.txtRows.Location = new System.Drawing.Point(554, 34);
this.txtRows.Name = "txtRows";
this.txtRows.Size = new System.Drawing.Size(132, 28);
this.txtRows.TabIndex = 5;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.lblResult);
this.groupBox1.Font = new System.Drawing.Font("华文中宋", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.groupBox1.Location = new System.Drawing.Point(80, 147);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(625, 316);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "生成区:";
//
// lblResult
//
this.lblResult.AutoSize = true;
this.lblResult.Location = new System.Drawing.Point(271, 48);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(0, 24);
this.lblResult.TabIndex = 0;
//
// Isosceles
//
this.Isosceles.AutoSize = true;
this.Isosceles.Font = new System.Drawing.Font("楷体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.Isosceles.Location = new System.Drawing.Point(554, 96);
this.Isosceles.Name = "Isosceles";
this.Isosceles.Size = new System.Drawing.Size(161, 28);
this.Isosceles.TabIndex = 7;
this.Isosceles.Text = "等腰三角形";
this.Isosceles.UseVisualStyleBackColor = true;
//
// YangHui
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 559);
this.Controls.Add(this.Isosceles);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.txtRows);
this.Controls.Add(this.lblTitle);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnForm);
this.Name = "YangHui";
this.Text = "杨辉三角";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnForm;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Label lblTitle;
private System.Windows.Forms.TextBox txtRows;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label lblResult;
private System.Windows.Forms.CheckBox Isosceles;
}
③运行结果如下(旧的void返回类型方法,不影响功能的正常实现):
Windows杨辉三角——使用Clipchamp制作
如果觉得对你有所帮助的话希望可以得到你的点赞和评论,以及欢迎大佬们(假如真的有人看的话)对本大一菜鸟的代码进行指导TAT