C#小练习



将百分制转化为五分制,若输入的百分制成绩超出0-100,程序抛出异常

需要用户自定义异常类OverflowRange,通过Throw new OverflowRange来实现

结果如图示:

代码:

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

namespace _71
{
    class overflowRange:ApplicationException///声明新的异常类
    {
        public overflowRange(string msg) : base(msg) { }///声明带参数的构造函数,并向基类传递参数
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("请输入一个0-100之内的分数");
                double x = double.Parse(Console.ReadLine());
                if (x < 0)
                    throw new overflowRange("百分制分数不能小于0!");
                else if (x > 100)
                    throw new overflowRange("百分制分数不能小于100!");
                Console.WriteLine("百分制转化为五分制后分数是:{0}", x / 20);
            }
            catch (FormatException)///捕获数据类型转换异常
            {
                Console.WriteLine("必须输入数字!");
            }
            catch (overflowRange e)///捕获自定义异常
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)///最后捕获其他类型的异常
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
        }
    }
}







委托实例

定义骑自行车的方法DriverBicycle()和骑摩托车的方法DriveMotorcycle()方法,这两个方法能够实现某人到达某地的骑行方式,用委托实现

结果如图:

代码:


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

namespace 委托实例412
{
    delegate string go(string people,string place);///声明
    class MyClass
    {
        public static string DriveBicycle(string driver, string destination)
        {
            return driver += " go to " + destination + " by Bicycle";
        }
        public static string DriveMotorcycle(string driver, string destination)
        {
            return driver += " go to " + destination + " by DriveMotorcycle";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            go d = new go(MyClass.DriveMotorcycle);///创建对象
            Console.WriteLine(d("Jack", "school"));///调用
            d += new go(MyClass.DriveBicycle);
            Console.WriteLine(d("Mike", "university"));
            Console.ReadKey();
        }
    }
}






接口:

如图:

代码:


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

namespace 接口实例414
{
    public interface IPerson///定义了接口
    {
        void eat();
        void sleep();
    }
    public class Student : IPerson///Student类继承了接口IPerson
    {
        public void eat()
        {
            Console.WriteLine("去餐厅吃");
        }
        public void sleep()
        {
            Console.WriteLine("回宿舍睡觉");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student st = new Student();
            st.eat();
            st.sleep();
            Console.ReadKey();
        }
    }
}





枚举:

如图:

代码:


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

namespace 枚举实例
{
    enum Color { Red, Yellow, Green }
    class TrafficLight
    {
        public static void WhatInfo(Color color)
        {
            switch ((int)(color))
            {
                case 0:
                    Console.WriteLine("Stop");
                    break;
                case 1:
                    Console.WriteLine("Warning");
                    break;
                case 2:
                    Console.WriteLine("Go");
                    break;
                default:
                    Console.WriteLine("有误");
                    break;
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Color c = Color.Red;
            Console.WriteLine(c.ToString());
            TrafficLight.WhatInfo(c);
            Console.ReadKey();
        }
    }
}






泛型类:

如图:

代码:

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

namespace 泛型类
{
    class MySwap<T>
    {
        public void Swap(ref T x, ref T y)
        {
            T temp;
            temp = x;
            x = y;
            y = temp;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            double a = 12.5, b = 7.5;
            double x = 1.2, y = 3.4;
            MySwap<double> ms = new MySwap<double>();
            ms.Swap(ref a,ref b);
            Console.WriteLine("交换之后:a={0},b={1}",a,b);
            ms.Swap(ref x, ref y);
            Console.WriteLine("交换之后:x={0},y={1}", x, y);
            Console.ReadKey();
        }
    }
}








泛型结构:

如图:

代码:


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

namespace 泛型结构
{
    struct Point<T>
    {
        public T x;
        public T y;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Point<double> point = new Point<double>();
            point.x = 10.1;
            point.y = 11;
            Console.WriteLine("x={0},y={1}",point.x,point.y);
            Point<int> p = new Point<int >();
            p.x = 1;
            p.y = 2;
            Console.WriteLine("x={0},y={1}", p.x, p.y);
            Console.ReadKey();
        }
    }
}







泛型方法:

如图:

代码:

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

namespace 泛型方法
{
    class MyClass
    {
        public static void Swap<T>(ref T x, ref T y) where T : IComparable  
        {
            T t;
            t = x;
            x = y;
            y = t;
            Console.WriteLine("x={0},y={1}",x,y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int x= 2, y = 3;
            MyClass.Swap<int>(ref x,ref y);
            double a = 2.3, b = 3.4;
            MyClass.Swap<double>(ref a, ref b);
            string m = "abc", n = "xyz";
            MyClass.Swap<string>(ref m, ref n);
           Console.ReadKey();
        }
    }
}








泛型方法:

创建一个静态类,在其中自定义一个泛型方法,实现查找数组元素的功能

如图:

代码:

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

namespace 泛型方法实现元素查找
{
    class Find
    {
        public static void find<T>(T x, T[] a)where T:IComparable
        {
            for (int i = 0; i <a.Length; i++)
            {
                if (a[i].Equals(x))
                {
                    Console.WriteLine("要查找的元素位置是第{0}个",i+1);
                }
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int x=3;
            int []a={2,3,4,7,6};
            Find.find<int>(x,a);
            Console.ReadKey();
        }
    }
}






泛型约束:

运行如图:

代码:

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

namespace 泛型约束
{
    class Myclass
    {
        public static void Sort<T>(T s) where T : ArrayList///泛型约束,T适用于ArrayList集合
        {
            s.Sort();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();///申请一个动态数组
            int[] a = {1,2,3,6,5,4 };
            al.AddRange(a);///将a加入动态数组
            Myclass.Sort<ArrayList>(al);///将动态数组元素排序
            foreach (int item in al)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值