首先创建一个Windows窗体应用
从左侧栏工具箱中拖拽一个button 一个textbox,一个label控件
按照要求置放位置,以及更改text属性,将textbox控件的“Multiline”
属性设为true,允许多行显示。
namespace Lab3_01_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Random r = new Random();
int[] array = new int[3] { r.Next(100), r.Next(100), r.Next(100) };
textBox1.Text = "随机生成的3个数为:"+array[0].ToString()+","+array[1].ToString() + "," + array[2].ToString()+"\r\n";
int Max;
Sort S = new Sort();
Max = S.Sort_1(array[0], array[1], array[2]);
textBox1.Text += "值参数方法,最大值为:" + Max.ToString() + "\r\n";
S.Sort_2(ref array[0], ref array[1], ref array[2]);
textBox1.Text += "输出参数方法,最大值为:" + array[2].ToString() + "\r\n";
S.Sort_3(array[0], array[1], array[2], out Max);
textBox1.Text += "输出参数方法,最大值为:" + Max.ToString() + "\r\n";
S.Sort_4(out Max, array);
textBox1.Text += "参数数组方法,最大值为:" + Max.ToString() + "\r\n";
}
}
class Sort
{
public int Sort_1(int x, int y, int z)
{
int tmp;
if (x > y) { tmp = x; x = y; y = tmp; }
if (x > z) { tmp = x; x = z; z = tmp; }
if (y > z) { tmp = y; y = z; z = tmp; }
return z;
}
public void Sort_2(ref int x, ref int y, ref int z)
{
int tmp;
if (x > y) { tmp = x; x = y; y = tmp; }
if (x > z) { tmp = x; x = z; z = tmp; }
if (y > z) { tmp = y; y = z; z = tmp; }
}
public void Sort_3(int x, int y, int z, out int max)
{
int tmp;
if (x > y) { tmp = x; x = y; y = tmp; }
if (x > z) { tmp = x; x = z; z = tmp; }
if (y > z) { tmp = y; y = z; z = tmp; }
max = z;
}
public void Sort_4(out int max, params int[] a)
{
max = a[0];
for (int i = 1; i < a.Length; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
}
}
}
运行截图: