c#实验

学号和姓名

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable hst = new Hashtable();
            hst.Add(001,"张军");
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("请输入学号");
                string num = Console.ReadLine();
                int num1;
                int.TryParse(num ,out num1);
                Console.WriteLine("请输入姓名");
                string name = Console.ReadLine();
                if (!hst.Contains(num1))
                    {
                        Console.WriteLine("您要查询的学号不存在");
                        hst.Add(num1, name);
                    }
                else { Console.WriteLine("您要查询的学号已存在"); }
                
            }
            foreach (DictionaryEntry item in hst)
            {
                if ((int)item.Key % 2 != 0) { Console.WriteLine("{0},{1}",item.Key,item.Value ); }
            }
        }
    }
}
泛型查找数组元素

using System;  
using System.Collections.Generic;  
using System.Text;  
  
namespace ConsoleApplication1  
{  
    static class MyClass //静态类  
    {  
        public static void Search<T>(T x,T[] a) where T : System.IComparable//泛型方法及泛型约束,注意数组参数写在参数列表的最后  
        {  
            int i, flag = -1;  
            for (i = 0; i < 10; ++i)  
            {  
                if (a[i].CompareTo(x) == 0)  
                {  
                    flag = i;  
                    break;  
                }  
            }  
            Console.WriteLine(flag);  
        }  
    }  
  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Console.WriteLine("请输入10个元素值:");  
            int[] a = new int[10];  
            for (int i = 0; i < 10; ++i)  
            {  
                int m = int.Parse(Console.ReadLine());  
                a[i] = m;  
            }  
            Console.Write("待查找的元素 x=");  
            int x = int.Parse(Console.ReadLine());  
            Console.Write("元素x的位置是:");  
            MyClass.Search<int>(x, a);  
            Console.ReadKey();  
        }  
    }  
}  
题库试题

using System;    
using System.Collections.Generic;    
using System.Linq;    
using System.Text;    
using System.Threading;    
namespace fengyun    
{    
    class Program    
    {    
        static void Main(string[] args)    
        {    
            int [] arrayKT={10,13,18,19,20,22,30,31};    
            getKTH(5, arrayKT);    
            Console.ReadKey();    
        }    

        public static string getKTH(int n,params int [] arrayKT)    
        {    
              //提示:主体中使用random类     
            Random rd = new Random();    
            Console.Write("从题库中抽出的题为:");    
            for (int j = 0; j < n; j++)    
            {    
                int index = rd.Next(0, arrayKT.Length);    
                Thread.Sleep(1000); //延时的办法的方法来避免Random快速连续产生相同随机数     
                Console.Write(" {0}",arrayKT[index]);    
            }    
            return "";    
        }    
    }    
}    
窗体实验之0~9数字输入

<pre name="code" class="csharp">using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;  
  
namespace WindowsApplication2  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void label1_Click(object sender, EventArgs e)  
        {  
  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            Application.Exit();//单击结束按钮程序即可结束  
        }  
  
        private void textBox1_TextChanged(object sender, EventArgs e)  
        {  
  
        }  
  
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
        {  
  
            textBox1.MaxLength = 8;  //最多输入8个数字  
            if (e.KeyChar < '0' || e.KeyChar > '9')文本框只能输入0至9这十种数字  
            {  
                e.Handled = true;  
            }  
        }  
    }  
}  

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 Form1_Load(object sender, EventArgs e)
        {
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox1.MaxLength = 8;
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar <'0' ||e.KeyChar >'9')
                e.Handled = true;
        }
    }
}

移动动画

 
<pre name="code" class="csharp">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 第8章  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            Close();  
        }  
  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            Timer t = new Timer();  
  
        }  
  
        private void timer1_Tick(object sender, EventArgs e)  
        {  
            Random r=new Random();  
            int t = r.Next(0, pictureBox1.Size.Height);  
            int l = r.Next(0, pictureBox1.Size.Width);  
            pictureBox1.Top = t;  
            pictureBox1.Left = l;  
  
        }  
  
  
    }  
}  

文件流读写
 
<pre name="code" class="csharp">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;  
using System.IO;  
namespace _12_2  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            if (!File.Exists(textBox1.Text))//判断文件是否存在  
                label2.Text = "该文件不存在";  
            else  
            {  
                label2.Text = "该文件的内容如下: ";  
                //建立一个文件流  
                FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);  
                StreamReader m_streamReader = new StreamReader(fs);//关联文件流,用于读取文件  
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);  
                this.textBox2.Text = "";  
                string strLine = m_streamReader.ReadLine();//输入文件的下一行  
                while (strLine != null)  
                {  
                    this.textBox2.Text += strLine + "\n";//读一行,换一行  
                    strLine = m_streamReader.ReadLine();//读取下一行  
                }  
                m_streamReader.Close();  
            }  
        }  
  
        private void button2_Click(object sender, EventArgs e)  
        {  
             if (File.Exists(textBox1.Text))  
                label2.Text = "该文件不存在";  
            else  
            {  
                label2.Text = "写文件成功";  
                FileStream fs = new FileStream(textBox1.Text, FileMode.OpenOrCreate, FileAccess.Write);//建立新文件  
                StreamWriter m_streamWrite = new StreamWriter(fs);//制定写入流为fs  
                m_streamWrite.Flush();//清理缓存区  
                m_streamWrite.BaseStream.Seek(0, SeekOrigin.Begin);  
                m_streamWrite.Write(textBox1.Text);//把textBox1中的内容写入文件  
                m_streamWrite.Close();//关闭此文件  
            }  
        }  
    }  
}  


 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值