Net Core教程

C# 考题参考

本文主要是介绍C# 考题参考,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        范围异常 年龄异常 = new 范围异常(0, 100);
        范围异常 身高异常 = new 范围异常(50, 250);
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int age = int.Parse(textBox1.Text);
                int height = int.Parse(textBox2.Text);
                检查(age, 年龄异常);
                检查(height, 身高异常);
            }
            catch(范围异常 ey){
               MessageBox.Show (ey.ToString());
            }                
        }
        private void 检查(int value,范围异常 ex){
            if(ex.检查(value)==false)throw new 范围异常();
            
        }
    }
    
    class 范围异常:Exception
    {
        int 下界;
        int 上界;
        public 范围异常()       : base()
        {
            
        }
        public 范围异常(int 下界, int 上界):base()
        {
            this.下界=下界;
            this.上界 = 上界;
        }
        public override string ToString()
        {
            return "范围错误";
        }
        public bool 检查(int value)
        {
            if(value<下界||value>上界)
                return false;
            else 
                return true;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                A a = new A();
                a.f();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }
    }
    class A
    {
        public void f(){
            
            throw new Exception();
        }
    }
    
}
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
             try
             {
                 double y;
                 y = -5.6 % 4.6;
                 int[] a = new int[20];
                 a[30] = 90;                 
             }
             
             catch(IndexOutOfRangeException ey){
                 try
                 {
                     float x = float.Parse(textBox1.Text);
                     float y = float.Parse(textBox2.Text);
                     float z = (float)Math.Sqrt(x);
                     textBox3.Text = z.ToString();
                 }
                 catch (FormatException ez){
                     MessageBox.Show("错误z:" + ez.ToString());
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show("错误x:" + ex.ToString());

             }
        }        

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                float x = float.Parse(textBox1.Text);
                float y = float.Parse(textBox2.Text);
                float z = x / y;
                textBox4.Text = z.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.ToString());
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using abc.xyz;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            a.x = 9;//a是namespace ConsoleApplication1中的类A的对象
            abc.A aa = new abc.A();
            aa.y = 8;//aa是namespace abc中的类A的对象
            abc.xyz.A aaa = new abc.xyz.A();
            aaa.z = 18;//aaa是namespace abc.xyz中的类A的对象
            abc.xyz.X xx = new abc.xyz.X();
            xx.www = 1919;
            xx.www = 10;
            try{           
            
                int u = a.x / xx.www;
                Console.WriteLine("haha.");
            }
            catch(Exception ex){
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
    class A {
        public int x;
    }
    class B { 
    }

}
namespace abc
{
    namespace xyz
    {
        class A {
            public int z;
        }
        class X
        {
            public int www;
        }
    }
    class A
    {
        public int y;
    }
}
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        学生 zs = new 学生();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            zs.姓名 = textBox4.Text;
            //button1.Text = zs.姓名;
            textBox1.Text = zs.姓;
            textBox2.Text = zs.名;
            textBox3.Text = zs.字;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            zs.姓名 = textBox4.Text;
            textBox1.Text = zs.姓名;
            
        }
    }

    class 学生
    {
        public string 姓;
        public string 名;
        public string 字;        
        public string 姓名
        {
            get
            {
                return "姓:"+姓+",名:"+名+",字:"+字;
            }
            set
            {
                string s = value;
                int 逗号位置;
                
                逗号位置=s.IndexOf(',');
                //逗号位置 = s.IndexOf("8787");
                if(s.Substring(0,2)=="诸葛" || s.Substring(0,2)=="东方" )
                {
                    姓 = s.Substring(0,2);//value的姓取出来;前两个字
                    名 = s.Substring(2, 逗号位置 - 2);//value的名取出来;第二字开始到半角逗号结束
                }

                else
                {
                    姓 = s.Substring(0,1);//value的姓取出来;第一个字
                    名 = s.Substring(1, 逗号位置 - 1);//value的名取出来;第二字开始到半角逗号结束
                }
                
                字 = s.Substring(逗号位置+1);//value的字取出来;半角逗号开始到结束
            }
        }

    }
}
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            男人类 tom = new 男人类("汤姆", "男", 20);
            女人类 mary = new 女人类("玛丽", "女", 60);
            textBox1.Text = tom.打印() + " " + tom.当膀爷();
            textBox2.Text = mary.打印()+" "+  mary.生孩子();

        }
        
    }
    public class 人类{
        public string name;
        public string sex;
        public int age;
        public 人类()
        {
        }
        public 人类(string name,string sex,int age) 
        {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }
        public string 打印(){
            return name+ " " +sex +" "  + age.ToString();
        }    
    }
    public class 男人类:人类
    {
        public 男人类(string name, string sex, int age)
        {
            //base(name, sex, age);
            base.name = name;
            base.sex = sex;
            base.age = age;
        }
        public string 当膀爷()
        {
            return "光膀子上街";
        }

    }
    public class 女人类 : 人类
    {
         public 女人类(string name, string sex, int age)
        {
            base.name = name;
            base.sex = sex;
            base.age = age;
        }

        public string 生孩子() {
            return "生一个孩子";
        }

    }

}

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            abc x = new abc();
            abc y = new abc("This is a string");
            abc[] a = new abc[5];
            for (int i=0;i<5;i++)
                a[i] = new abc("haha");
            Console.ReadLine();
        }
    }
    class abc 
    {
        public abc() { 
            Console.WriteLine ("abc");
        }
        public abc(string s)                
        {
            Console.WriteLine(s);
        }
    }

}


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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;
            int x;
            动物[] arr1 = new 动物[4];
            for (i = 0; i < 4; i++)
            {
                Console.WriteLine("第" + i.ToString() + "个动物是什么? (1:狗,2:猫,3:大象,4:人)");
                x = int.Parse(Console.ReadLine());
                switch (x)
                {
                    case 1:
                        arr1[i] = new 狗();
                        break;
                    case 2:
                        arr1[i] = new 猫();
                        break;
                    case 3:
                        arr1[i] = new 大象();
                        break;
                    case 4:
                        arr1[i] = new 人();
                        break;
                    default:
                        Console.WriteLine("输入错误");
                        arr1[i] = new 人();
                        break;
                }

            }           
            

            for ( i = 0; i < 4; i++)
            {
                arr1[i].吃();
            }
            Console.ReadLine();
        }


    }

    class 动物 {
        public virtual void 吃()
        {
            //Console.WriteLine("动物都要吃。");
        }
    }

    class 狗 : 动物
    {
        public override void 吃()
        {
            Console.WriteLine("狗啃骨头");
        }
    }
    class 猫 : 动物
    {
        public override void 吃()
        {
            Console.WriteLine("猫吃鱼");
        }
    }
    class 大象 : 动物
    {
        public override void 吃()
        {
            Console.WriteLine("大象吃草或树叶");
        }
    }
    class 人 : 动物
    {
        public override void 吃()
        {
            Console.WriteLine("人什么都吃,天上飞的没有吃过蚊子,四条腿的没有吃过板凳。");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            C c = new C();
            Console.ReadLine();
        }
    }
    class A {
        public A()
        {
            Console.WriteLine("A");
        }
    }
    class B
    {
        public B()
        {
            Console.WriteLine("B");
        }
    }

    class C : A
    {
        B b = new B();
        public C()
        {
            Console.WriteLine("C");
        }
    }
}
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            三维坐标 坐标1 = new 三维坐标(float.Parse (textBox1.Text), float.Parse(textBox2.Text), float.Parse(textBox3.Text));
            三维坐标 坐标2 = new 三维坐标(float.Parse(textBox4.Text), float.Parse(textBox5.Text), float.Parse(textBox6.Text));
            三维坐标 坐标3 = new 三维坐标();
            坐标3 = 坐标1 + 坐标2;
            textBox7.Text = (坐标3.x).ToString();
            textBox8.Text = (坐标3.y).ToString();
            textBox9.Text = (坐标3.z).ToString ();

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
    class 三维坐标 {
        public float x,y,z;
        public 三维坐标()
        {
        }
        public 三维坐标(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public static 三维坐标 operator ++(三维坐标 p1)
        {
            return new 三维坐标(p1.x +1, p1.y + 1, p1.z + 1);
        }
        public static 三维坐标 operator --(三维坐标 p1)
        {
            return new 三维坐标(p1.x - 1, p1.y - 1, p1.z - 1);
        }
        public static 三维坐标 operator +(三维坐标 p1, 三维坐标 p2)
        {
            return new 三维坐标(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z);
        }
        public static 三维坐标 operator -(三维坐标 p1, 三维坐标 p2)
        {
            return new 三维坐标(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z);
        }
    }
}
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            复数 c1 = textBox1.Text;
            复数 c2 = textBox2.Text;
            //复数 c3 = c1 - c2;
            textBox3.Text = c1.ToString();

        }
    }
    class 复数 {
        double 实部,虚部;
        public 复数()
        {
            实部 = 0;
            虚部 = 0;
        }
        public 复数(double 实部,double 虚部)
        {
            this.实部 = 实部;
            this.虚部 = 虚部;
        }
        public override string ToString()
        {
            return 实部+"+"+虚部+"i";
        }
        public static implicit operator 复数(string 复数字符串)
        {
            if (复数字符串 == "")
            {
                复数 转换后的复数 = new 复数();
                return 转换后的复数;
            }
            else
            {
                string[] 实部和虚部数字 = 复数字符串.Split('+', '-', 'i', '*');
                //MessageBox.Show(实部和虚部数字[0] + " " + 实部和虚部数字[1]);
                复数 转换后的复数 = new 复数(double.Parse(实部和虚部数字[0]), double.Parse(实部和虚部数字[1]));
                return 转换后的复数;
            }
        }
    }

}
这篇关于C# 考题参考的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!