程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#基礎復習練習

c#基礎復習練習

編輯: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();//不同命名空間下使用對方的類,需要引用對方的命名空間,命名空間是可以修改的,方便把類放到不同的文件夾下,改了命名空間,引用也要跟著變化www.2cto.com
        }
    }
}

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

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) + "來的人";
        }
    }
}
 


摘自  蝸牛的奮斗
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved