• c#基础复习练习


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 常量
    {
        class Program
        {
            public const int pi = 3;//常量在所有函数中都可调用,不用new
            static void Main(string[] args)
            {
                const int ppp = 222;//局部常量
            }
        }
    }

    ----------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 对象的引用
    {
        class Program
        {
            static void Main(string[] args)
            {
                person i = new person();
                person j = i;
                i.Zage(30);
                i.Age++;
                Console.WriteLine(j.Age);//对象是引用类型 传递的是引用
                Console.ReadKey();
            }
        }
        class person
        {
            public int Age
            { get; set; }
            public void Zage(int age)
            {
                this.Age = age;
            }
        }
    }

    ---------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 构造函数
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person i = new Person();//构造函数的作用就是创建对象的同时对其初始化,更好的显现了封装性
                Person j = new Person("lily");
                Person k = new Person("pangzi",12);
                Console.WriteLine("我的名字是{0}我今年{1}岁",i.Name,i.Age);
                Console.WriteLine("我的名字是{0}我今年{1}岁", j.Name, j.Age);
                Console.WriteLine("我的名字是{0}我今年{1}岁", k.Name, k.Age);
                Console.ReadKey();
            }
        }
        class Person
        {
            public string Name { get; set; }
            public int Age { get;set;}
            public Person()
            {
                Name = "无名";
                Age=10000;
            }
            public Person(string name)
            {
                this.Name = name;
                Age = 111;
            }
            public Person(string name,int age)
            {
                this.Name = name;
                this.Age = age;
            }
        }

    }

    --------------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 静态成员
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person.name = "asksadasd";//静态成员直接可以调用,不可以new
                Console.WriteLine(Person.name);
                Person ren = new Person();//非静态需要创建对象进行调用不可直接调用
                ren.age = 33;
                ren.Sayhello();
                Person.Want();
                Console.WriteLine(ren.age);
                Huairen.age = 55;
                //Huairen e = new Huairen();静态类是不能被实例话的。
                Console.ReadKey();
            }
        }
        class Person
        {
            public static string name;//静态成员不需要NEW就可直接调用,可以看做是全局变量
            public int age;//非静态成员需要new一个对象进行调用
            public void Sayhello()
            {
                Console.WriteLine("姓名{0}年龄{1}",name,age);//在非静态成员中可以调用静态成员
            }
            public static void Want()
            {
               // Console.WriteLine("姓名{0}年龄{1}",name,age);在静态成员中不可以调用非静态成员,要求对象引用
                Person a = new Person();
                a.age = 22;
                Console.WriteLine("姓名{0}年龄{1}", name,a.age);
            }
        }
        static class Huairen
        {
            public static int age;
           
        }
    }

    ------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 类
    {
        class Program
        {
            static void Main(string[] args)
            {
                person p = new person();
                p.age = 12;
                p.name = "lily";
                p.Sayhello();
                person1 b = new person1();
                b.Age = 20;//属性只是控制的作用,封装性,储存的是字段
                b.Name = "tom";
                b.Say();
                person2 c = new person2();
                c.Age = 30;
                Console.WriteLine(c.Age);//输出结果是3,只是可读的,属性不能储存数据
                Console.ReadKey();
            }
        }
        class person2
        {
            private int age;
            public int Age
            {
                set
                {
                }
                get
                {
                    return 3;
                }
            }
        }
        class person
        {
            public string  name;
            public int age;
            public void Sayhello()
            {
                Console.WriteLine("大家好,我叫{0},今年{1}岁了!",this.name,this.age);
            }

        }
        class person1
        {
            private string name;
            public string Name
            {
                set
                {
                    if (value != "tom")
                    {
                        return;
                    }
                    else
                    {
                        this.name = value;
                    }
                }
                get
                {
                    return name;
                }
            }
            private int age;
            public int Age
            {
                set
                {
                    if (value <= 0)
                    {
                        return;
                    }
                    else
                    {
                        this.age = value;
                    }
                }
                get
                {
                    return age;
                }
            }
            public void Say()
            {
                Console.WriteLine("大家好我是{0}我今年{1}",this.name,this.age);
            }
        }
    }

    ---------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 函数
    {
        class Program
        {
           
           
            static void Main(string[] args)
            {   //练习1利用函数计算两个数的和
                /*
                Console.WriteLine(Max(2,5));
                Console.ReadKey();
                */
                //练习2 利用函数计算数组的和
                /*
                int[] values = { 2,3,4,5,6,2,4,5,2,1,4,5};
                int sum = values.Sum();还可以使用效果int sum=Sum(values); 是一样的 
                Console.WriteLine(sum);
                Console.ReadKey();
                */
                //练习 3 用“|”分割数组,并形成新的字符串
                /*
                string[] name = { "aa","bb","cc"};
                string newstrings = Join(name,"|");
                Console.WriteLine(newstrings);
                 */
                //练习4 可变参数
                /*
                Hey("a","b","c","d","e","f","g");
                Sayhello("大帅哥","sad","sd","dd");//可以与非可变参数合用,如果不是可变参数,就得使用重载,可变参数必须放在形参最后一个参数
                 */
                //练习5 函数重载,函数名相同,函数的类型和个数不同
                /*
                Hello();
                Hello("小明");
                Hello("小明",8);
                 */
                //练习6  字符串练习
                /*
                string s="HHH";
                Console.WriteLine(s.ToLower());//转换成小写
                Console.WriteLine(s.ToUpper());//转换成大写
                string b = "    a   b   ";
                Console.WriteLine(b.Trim());//它只会去掉2边的空白,中间的它不管
                bool c = s.Equals("hhh",StringComparison.OrdinalIgnoreCase);//字符串比较,ignore忽略 case 大小写,==是区分大小写的比较
                Console.WriteLine(c);
                string n="sss,ss,ff,fg,sd,aws,sdf,tht,xc|ttt[fassaf]fasdsf";
                string[] name = n.Split(',');//用字符,进行分割字符串
                name = n.Split(',','|','[',']');//是可变参数,可以利用多个分割符进行拆分
                foreach(string i in name)
                {
                    Console.Write(i);
                }
                string j = "tht,xc|ttt[fassaf]fas,,,,,,,,dsf";
                string [] hello = j.Split(new char[]{ ',' }, StringSplitOptions.RemoveEmptyEntries);//用,分割字符串,并清除字符串中的空字符
                foreach (string p in hello)
                {
                    Console.WriteLine(p);
                }
                string k = "小狗 小猫  小鸡 小兔 小虫子 小人 小花 小草";
                string[] kk = k.Split(new string[] {"小"}, StringSplitOptions.RemoveEmptyEntries);//这是字符串的使用形式
                foreach (string g in kk)
                {
                    Console.WriteLine(g);
                }
                 */
                //练习7 字符串处理
                /*
                string emal = Console.ReadLine();
                int atindex = emal.IndexOf('@');
                string name = emal.Substring(0, atindex);
                string aftername = emal.Substring(atindex+1);
                Console.WriteLine("{0}{1}",name,aftername);
                 */
                //练习8 聊天机器人
                /*
                Console.WriteLine("你好我是机器人");
                int full = 2;
                while(true)
                {  
                    if(full<=0)
                    {
                        Console.WriteLine("我快饿死了,没法聊了,要想聊,先喂饭,聊几次喂几次");
                        full = Convert.ToInt32(Console.ReadLine());
                        if(full<=0)
                        {
                            Console.WriteLine("玩我呢?生气了");
                            return;
                        }
                    }
                    Console.WriteLine("你想说什么呢?");
                    string say = Console.ReadLine();
                    if(say.Contains("天气"))
                    {
                        say = say.Substring(2);
                        Console.WriteLine("{0}天气不好",say);
                    }
                    else if(say.Contains("88")||say.Contains("再见"))
                    {
                        Console.WriteLine("再见");
                        return;
                    }
                    full--;
                }
                 */
                //练习9 ref out 参数应用
                int age = 20;//ref 是引用必须先初始化
                Intage(ref age);
                Console.WriteLine(age);//结果是21,没有应用ref参数的话结果还20,是复制不是引用,用了ref就是引用,还需注意ref需配对使用
                int outage;//out 不需要初始话,初始话了也没用,必须在函数内赋值,是内部为外部赋值,out一般用在函数有多个返回值的场所
                Outage( out outage);
                Console.WriteLine(outage);
                string str = Console.ReadLine();
                int i;
                if (int.TryParse(str, out i))
                {
                    Console.WriteLine("转换成功{0}", i);
                }
                else
                {
                    Console.WriteLine("转换失败");
                }
                int i1 = 10; int i2 = 20;
                swap(ref i1,ref i2);
                Console.WriteLine("{0}{1}",i1,i2);
                Console.ReadKey();

            }
            static void swap(ref int i1, ref int i2)
            {
                int temp = i1;
                i1 = i2;
                i2 = temp;
            }
            static void Outage(out int outage)
            {
                 outage = 80;
            }
            static void Intage(ref int age)
            {
                age++;
            }
            static void Hello()
            {
                Console.WriteLine("你好") ;
            }
            static void Hello(string name)
            {
                Console.WriteLine("{0}你好",name);
            }
            static void Hello(string name ,int age)
            {
                Console.WriteLine("{0}你好!你已经{1}岁了",name,age);
            }
            static void Sayhello(string name, params string[] names)
            {
                Console.WriteLine("我的名字是{0}",name);
                foreach(string nickname in names)
                {
                    Console.WriteLine("我的昵称是{0}",nickname);
                }
            }
            static void Hey(params string [] names)
            {
                foreach(string name in names)
                {
                    Console.WriteLine(name);
                }
            }
            static string Join(string [] name,string sperate)
            {
                string s = "";
                for (int i = 0; i < name.Length - 1;i++ )
                {
                    s=s+name[i]+sperate;
                }
                if(name.Length>0)
                {
                    s=s+name[name.Length-1];
                }
                return s;
            }
            static int Sum(int [] values)
            {
                int sum = 0;
                foreach(int i in values)
                {
                    sum = sum + i;
                }
                return sum;
            }
            static int Max(int i1,int i2)//计算2个数最大值
            {
                if(i1>i2)
                {
                    return i1;
                }
                else
                {
                    return i2;
                }
            }
        }
    }

    -----------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 继承
    {
        class Program
        {
            static void Main(string[] args)
            {
                中国人 c = new 中国人();
                c.Name = "周杰伦";
                c.Say();
                c.功夫();
                日本人 jb = new 日本人();
                jb.Name = "苍井空";
                jb.Say();//继承的
                jb.AV();//自己私有的

                Person ren=new Person();
                ren = c;//父类可以指向子类,但不能用子类私有的成员
                ren.Say();
                Person renyao = new Person();
                renyao = jb;
                jb = (日本人)renyao;//在父类对象没有指向其他类对象的情况下,子类可以用强制转换的方式指向父类
                jb.Say();
                jb= (日本人)ren;//中国人是人,怎么弄也不会变成日本人,ren指向的是中国人无法进行强制转换
                jb.Say();
                Console.ReadKey();
            }
        }
        class Person
        {
            public string Name { set; get; }
            public int Age { get; set; }
            public void Say()
            {
                Console.WriteLine("我是{0}",Name);
            }
        }
        class 中国人 : Person
        {
            public void 功夫()
            {
                Console.WriteLine("快使用双截棍哼哼哈嘿!!!");
            }
        }
        class 日本人 : Person
        {
            public void AV()
            {
                Console.WriteLine("亚麻带");
            }
        }
    }

    ------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 聊天机器人
    {
        class Program
        {
            static void Main(string[] args)
            {
                Robot ren = new Robot();
                Robot ren1 = new Robot();
                ren1.Name = "月亮";
                ren.Name = "地球";
                ren1.Eat(3);
                ren.Eat(2);
                Console.WriteLine("你想和地球对话还是原谅对话,选择地球按1选择月亮按2");
                Robot r;
                string i=Console.ReadLine();
                if (i == "1")
                {
                    r = ren;//r指向ren指向的对象
                }
                else
                {
                    r = ren1;
                }
                r.Say();
                while(true)
                {
                    Console.WriteLine("你想对我说什么呢?");
                    string str = Console.ReadLine();
                    r.Speak(str);
                }
            }
        }
        class Robot
        {
            private string name;
            public string Name
            {
                set
                {
                    this.name = value;
                }
                get
                {
                    return this.name;
                }
            }
            private int full;
            private int Full//饿不饿就自己知道,私有的,不是所有属性都可以对外公开的
            {
                set
                {
                    this.full = value;
                }
                get
                {
                    return this.full;
                }
            }
            public void Say()
            {
                Console.WriteLine("大家好我叫{0}",this.name);
            }
            public void Eat(int food)
            {
                if (full >= 20)
                {
                    return;
                }
                else
                {
                    full = full + food;
                }
            }
            public void Speak(string str)
            {  if( full<=0)
                {
                    Console.WriteLine("饿了不聊了,你想喂我吗?yes还是on");
                    string want = Console.ReadLine();
                    if (want == "yes")
                    {
                        Console.WriteLine("请喂食,想聊几次,喂几个食物!");
                        int food = Convert.ToInt32(Console.ReadLine());
                        if (food <= 0)
                        {
                            return;
                        }
                        Eat(food);
                    }
                    else
                    {
                        return;
                    }
                }
                if(str.Contains("你好"))
                {
                    Console.WriteLine("你好?有事吗?");
                }
                else if (str.Contains("女朋友") || str.Contains("喜欢你"))
                {
                    Console.WriteLine("我有男朋友了!");
                }
                else
                {
                    Console.WriteLine("听不懂你说什么?");
                }
                full--;
            }
        }
    }

    -------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 异常
    {
        class Program
        {
            static void Main(string[] args)
            {
               
                try
                {
                   
                    int b = Convert.ToInt32("nihao");
                }
                catch( Exception e)
                {
                    Console.WriteLine("数据错误"+e.Message+"异常堆帧:"+e.StackTrace);
                }
                
                try
                {
                    Age(400);
                }
                catch(Exception a)//接异常
                {
                    Console.WriteLine("数据错误:{0}",a.Message);//异常信息,一般情况下不需要抛异常
                }
                Console.ReadKey();
            }
            static void Age(int age)
            {
                if(age>=0&&age<=5)
                {
                    Console.WriteLine("幼儿");
                }
                else if (age>5&&age<19)
                {
                    Console.WriteLine("青少年");
                }
                else if(age>18&&age<150)
                {
                    Console.WriteLine("成年人");
                }
                else if(age<0)
                {
                    throw new Exception("你是外星人吧!!!!!!!");//抛异常
                }
                else if(age>149)
                {
                    throw new Exception("你见过毛主席吧!!!!!");
                }
            }
        }
    }

    ------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using lianxi3.cc;

    namespace lianxi3
    {
        class Program
        {
            static void Main(string[] args)
            {
                ArrayList a = new ArrayList();
                dd ss = new dd();//不同命名空间下使用对方的类,需要引用对方的命名空间,命名空间是可以修改的,方便把类放到不同的文件夹下,改了命名空间,引用也要跟着变化
            }
        }
    }

    -----------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 索引器
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    Person h = new Person();
                    h[1] = "三毛";
                    h[2] = "二毛";
                   
                    Console.WriteLine("{0}{1}{2}", h[1],h[2],h[3]);
                   
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.ReadKey();
            }
        }
        class Person
        {
            private string name = "小狗";
            private string name1 = "小猫";
            private string name2 = "小鸡";
            public string this[int index]//参数可以变的可以是多个
            {
                set
                {
                    if(index==1)
                    {
                        name = value;
                    }
                    else if(index==2)
                    {
                        name1 = value;
                    }
                    else if (index == 3)
                    {
                        this.name2 = value;
                    }
                    else
                    {
                        throw new Exception("错误");
                    }
                }
                get
                {
                   if(index==1)
                    {
                        return name;
                    }
                    else if(index==2)
                    {
                        return name1;
                    }
                   else if (index == 3)
                   {
                       return name2;
                   }
                   else
                   {
                       throw new Exception("错误");
                   }
                }
            }
        }
    }

    -------------------------------------winform阶段

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace textbox测试
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button2_Click(object sender, EventArgs e)
            {
                time.AppendText(DateTime.Now.ToString()+"\n");//推荐用这种,只是附加,下面那种是字符串的重叠相加
                //time.Text = time.Text + DateTime.Now.ToString() + "\n";
            }

            private void button1_Click(object sender, EventArgs e)
            {
                dantime.AppendText(DateTime.Now.ToString()+"\n");
            }
        }
    }

    -------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplicationEMAIL分析
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string Email = email.Text;
                string[] str = Email.Split('@');
                if (str.Length>2)//判断是否有2部分组成
                {
                    MessageBox.Show("地址不正确");
                    return;
                }
                username.Text=str[0];
                yuming.Text=str[1];
            }
        }
    }

    --------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication滚动
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string b = gundong.Text;
                char one = b[b.Length-1];
                string qita = b.Substring(0,b.Length-1);
                gundong.Text =one+qita;//每次点形成新的字符串,每次将最后一个变成第一个
            }

            private void button2_Click(object sender, EventArgs e)
            {
                string s = gundong.Text;
                char one=s[0];
                string qita = s.Substring(1);
                gundong.Text = qita + one;//每次形成新的字符串,每次将将第一个变成最后一个。
            }
        }
    }

    ---------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication累加
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string diyi = first.Text;
                string dier = sencond.Text;
                int i1, i2;
                if(int.TryParse(diyi,out i1)==false)
                {
                    MessageBox.Show("第一个数字格式不正确!");
                    return;
                }
                if(int.TryParse(dier,out i2)==false)
                {
                    MessageBox.Show("第二个数字格式不正确");
                    return;
                }
                if(i1>i2)
                {
                    MessageBox.Show("第二个数字必须比第一个大");
                    return;
                }
                int sum = 0;
                for (int i=i1; i <= i2;i++ )
                {
                    sum = sum + i;
                }
                jieguo.Text = Convert.ToString(sum);
            }
        }
    }

    ------------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication练习
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
                string name = textBox1.Text;
                this.Text = name+"你好";
            }

            private void button2_Click(object sender, EventArgs e)
            {
                string yi = one.Text;
                string er = two.Text;
                int i1,i2;
                if(int.TryParse(yi,out i1)==false)
                {
                    MessageBox.Show("第一个数不是整数!!!!!!");
                    return;//不要忘了return
                }
                if(int.TryParse(er,out i2)==false)
                {
                    MessageBox.Show("第二个数不是整数!!!!");
                    return;
                }
                jieguo.Text = Convert.ToString(i1+i2);
            }
        }
    }

    ----------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication图片
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string s = shenfen.Text;
                if (s.Length ==18) //身份证号是18位
                {
                    string h = s.Substring(6, 4);
                    int i;
                    if (int.TryParse(h, out i) == false)
                    {
                        MessageBox.Show("你输入的数据格式不正确!!!!");
                        return;
                    }
                    if ((DateTime.Now.Year - i) >= 18)
                    {
                        picture.Visible = true;
                    }
                    else
                    {
                        picture.Visible = false;
                        MessageBox.Show("对不起你是未成年人不得查看");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("不是合法的身份证号!");
                    return;
                }
            }
        }
    }

    -------------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace 登录
    {
        public partial class Form1 : Form
        {
            private int i = 0;
            public Form1()
            {
                InitializeComponent();
            }

            private void label1_Click(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
                string username = txtuser.Text.Trim();
                string password = txtpassword.Text;
                if (username.Equals("admin", StringComparison.OrdinalIgnoreCase) && (password == "888"))
                {
                    MessageBox.Show("登录成功!!!");
                }
                else
                {
                    i++;
                    if(i>=3)
                    {
                        MessageBox.Show("登录次数过多,程序即将退出!");
                        Application.Exit();
                    }
                    MessageBox.Show("登录失败!!");
                }
            }
        }
    }

    ---------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace 求最大成绩
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                int max = 0;
                string Zname = "";
                string[] lines = chengji.Lines;//将多行文本框的值,按行存储到数组中
                foreach(string line in lines)
                {
                    string[] s = line.Split('=');//每一行按=分割成2部分,前部分是名字,后部分是成绩
                    string name=s[0];
                    string chengji1=s[1];
                    int i;
                    if (int.TryParse(chengji1, out i) == false)
                    {
                        MessageBox.Show("格式不正确");
                    }
                    if (i > max)
                    {
                        max = i;
                        Zname = name;
                    }
                }
                jieguo.Text = "最大成绩是" + max + "获得者是" + Zname;
            }
        }
    }

    ---------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace 四则运算
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string shu1 = txtshu1.Text;
                string shu2 = txtshu2.Text;
                int i1, i2;
                int result;
                if(int.TryParse(shu1,out i1)==false)
                {
                    MessageBox.Show("数据错误");
                    return;
                }
                if (int.TryParse(shu2, out i2) == false)
                {
                    MessageBox.Show("数据错误");
                    return;
                }
                switch(cbsuan.SelectedIndex)
                {
                    case 0://+
                        result=i1+i2;
                        break;
                    case 1://-
                        result = i1 - i2;
                        break;
                    case 2://*
                        result = i1 * i2;
                        break;
                    case 3:// /
                        if(i2==0)
                        {
                            MessageBox.Show("被除数不能为0");
                            return;
                        }
                        result = i1 / i2;
                        break;
                    default:
                        throw new Exception ("没有此类运算符");
                      
                }
                txtresult.Text = Convert.ToString(result);
            }
        }
    }

    ----------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace 下来列表
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(Convert.ToString(cbname.SelectedIndex));//选中的序号
                MessageBox.Show(Convert.ToString(cbname.SelectedText));
                MessageBox.Show(Convert.ToString(cbname.SelectedValue));
                MessageBox.Show(Convert.ToString(cbname.SelectedItem));//选中的内容
            }
        }
    }

    ------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace 修改密码
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                string oldpassword = txtoldpassword.Text;
                string newpassword = txtnewpassword.Text;
                string newpassword2 = txtnewpassword2.Text;
                if(oldpassword!="888")
                {
                    MessageBox.Show("旧密码不正确");
                    return;
                }
                if(newpassword!=newpassword2)
                {
                    MessageBox.Show("2次密码不一致");
                    return;
                }
                if (oldpassword == newpassword)
                {
                    MessageBox.Show("新密码不能和旧密码一样");
                    return;
                }
                MessageBox.Show("修改成功");
            }
        }
    }

    ---------------------------------------

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace 选择省找寻市
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void cb省_SelectedIndexChanged(object sender, EventArgs e)
            {
                cb市.Items.Clear();
                string sheng = Convert.ToString(cb省.SelectedItem);
                if(sheng=="山东")
                {
                    cb市.Items.Add("济南");
                    cb市.Items.Add("临沂");
                  
                }
                if (sheng == "辽宁")
                {
                    cb市.Items.Add("鞍山");
                    cb市.Items.Add("大连");
                   
                } if (sheng == "江苏")
                {
                    cb市.Items.Add("徐州");
                    cb市.Items.Add("连云港");
                   
                }

            }

            private void cb市_SelectedIndexChanged(object sender, EventArgs e)
            {
                cb区.Items.Clear();
                string shi = Convert.ToString(cb市.SelectedItem);
                if (shi == "济南")
                {
                    cb区.Items.Add("历下区");
                    cb区.Items.Add("槐荫区");

                }
                if (shi == "临沂")
                {
                    cb区.Items.Add("兰山区");
                    cb区.Items.Add("临港经济开发区");

                } if (shi == "鞍山")
                {
                    cb区.Items.Add("码头");
                    cb区.Items.Add("山头");

                }
                if (shi == "大连")
                {
                    cb区.Items.Add("碍事");
                    cb区.Items.Add("地方");

                }
                if (shi == "徐州")
                {
                    cb区.Items.Add("吊死鬼");
                    cb区.Items.Add("辅导");

                }
                if (shi == "连云港")
                {
                    cb区.Items.Add("多少");
                    cb区.Items.Add("发到");

                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                dizhi.Text = Convert.ToString(cb省.SelectedItem) + Convert.ToString(cb市.SelectedItem) + Convert.ToString(cb区.SelectedItem) + "来的人";
            }
        }
    }

  • 相关阅读:
    Xml 和Json解析
    Block高级使用 传值
    UIImagePickerController
    单例传值
    eclipse常用快捷键
    sql去掉多条重复只显示一条
    oracle 游标使用
    oracle 去掉重复的数据
    oracle 把一行用特殊符号分隔变为多行
    oracle 去掉锁表
  • 原文地址:https://www.cnblogs.com/liujiaoxian/p/2393254.html
Copyright © 2020-2023  润新知