首页 > 其他 > 详细

C#学习笔记(2)

时间:2014-03-01 04:56:55      阅读:472      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace demo2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
             Random r = new Random();				// 实例化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();
        }
    }
    class Sort									// 声明一个类Sort
    {
        public int Sort_1(int x, int y, int z)     			// 值参数方法
        {
            int tmp;                              	// tmp是方法Sort_1的局部变量
            // 将x,y,z按从小到大排序
            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;							// 返回最大值z
        }
        public void Sort_2(ref int x, ref int y, ref int z) 		// 引用参数方法
        {
            int tmp;                            	   	// tmp是方法Sort_2的局部变量
            // 将x,y,z按从小到大排序
            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;                            		// tmp是方法Sort_3的局部变量
            // 将x,y,z按从小到大排序
            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; 							// 最大值z赋给参数max
        }
        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]; }
        }
   }
}

bubuko.com,布布扣

C#学习笔记(2),布布扣,bubuko.com

C#学习笔记(2)

原文:http://blog.csdn.net/jkxqj/article/details/20136645

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!