C#操作6-10
第六题结构类型数据
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 第六题 { struct Student { string name; public string Name { get { return name; } set { name = value; } } string sex; public string Sex { get { return sex; } set { sex = value; } } int age; public int Age { get { return age; } set { age = value; } } int height; public int Height { get { return height; } set { height = value; } } int weight; public int Weight { get { return weight; } set { weight = value; } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 第六题 { class Program { static void Main(string[] args) { Student stu1 = new Student(); stu1.Name = "张三"; stu1.Age = 21; stu1.Height = 180; stu1.Weight = 130; stu1.Sex = "男"; } } }
第七题建立一个标签
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) { this.textBox1.Text = this.label1.Text; } } }
第八题成绩评优
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 WindowsForms { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btConfirm_Click(object sender, EventArgs e) { int score; int n; score = Convert.ToInt32(tbScore.Text); n = score / 10; switch(n) { case 10: case 9: lbgraded.Text="成绩为优秀!"; break; case 8: lbgraded.Text="成绩为良好!"; break; case 7: lbgraded.Text="成绩为中等!"; break; case 6: lbgraded.Text="成绩及为格!"; break; case 5: case 4: case 3: case 2: case 1: case 0:lbgraded.Text="成绩为不及格!";break; default:MessageBox.Show("输入的成级超过了范围,请输入!","提示框");break; } } } }
第九题MyCalss类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 第九题 { class MyClass { #region 定义 public 类成员 public string publicVar; public void publicMethod() { Console.WriteLine("您现在调用的是public类型的方法!"); } #endregion #region 定义 protected 类成员 protected string protectedVar; protected void protectedMethod() { Console.WriteLine("您现在调用的是protected类型的方法!"); } #endregion #region 定义 private 类成员 private string privateVar; private void privateMethod() { Console.WriteLine("您现在调用的是private类型的方法!"); } #endregion } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 第九题 { class MyMain:MyClass { public static void Main() { MyClass obj = new MyClass(); //不会产生错误 //1访问public修饰的成员 obj.publicMethod(); obj.publicVar = "test1"; //将会产生编译错误,无法运行 //2访问protected修饰的成员 //obj.protectedMethod(); //obj.protectedVar = "test1"; //将会产生编译错误,无法运行 //3访问private修饰的成员 //obj.privateMethod(); //obj.privateVar = "test1"; } } }
第十题服务器地址
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data.OleDb; namespace 第十题 { class Program { static void Main(string[] args) { string strCon = "server =202.196.131.26; database=test; uid =sa; pwd=administrator"; SqlConnection connection = new SqlConnection(strCon); //获取当前应用程序的路径 string parth = Environment.CurrentDirectory; string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+parth+" \db\jobtypeDB.mdb"; OleDbConnection connection = new OleDbConnection(strCon); } } }