基础知识复习+练习(带*为选做)
以下练习题做的时候如果遇到不会做的,请跳过本题继续向后做。
- 编写一段程序,运行时向用户提问“你考了多少分?(0~100)”,接受输入后判断其等级并显示出来。判断依据如下:等级={优 (90~100分);良 (80~89分);中 (60~69分);差 (0~59分);}
- 编程输出九九乘法表。
- 定义长度50的数组,随机给数组赋值,并可以让用户输入一个数字n,按一行n个数输出数组 int[] array = new int[50]; Random r = new Random(); r.Next();
- 编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成小写然后返回(命名规范 骆驼命名) name s.SubString(0,1) s.SubString(1);
- 编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成大小然后返回(命名规范 帕斯卡)
- 声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。扩展(*):不使用第三个变量如何交换?
- 用方法来实现:将上题封装一个方法来做。提示:方法有两个参数n1,n2,在方法中将n1与n2进行交换,使用ref。(*)
- 请用户输入一个字符串,计算字符串中的字符个数,并输出。
- 用方法来实现:计算两个数的最大值。思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:params)。
- 用方法来实现:计算1-100之间的所有整数的和。
- 用方法来实现:计算1-100之间的所有奇数的和。
- 用方法来实现:判断一个给定的整数是否为“质数”。
- 用方法来实现:计算1-100之间的所有质数(素数)的和。
- 用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。
- 用方法来实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。
- 请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。
- 为教师编写一个程序,该程序使用一个数组存储30个学生的考试成绩,并给各个数组元素指定一个1-100的随机值,然后计算平均成绩。
- 有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:请统计出该字符中“咳嗽”二字的出现次数,以及每次“咳嗽”出现的索引位置。
- 将字符串" hello world,你 好 世界 ! "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,输出结果为:"hello world,你 好 世界 !"。
- 制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数,以及每个学生的姓名。效果如图:
- 题目内容同上题,再增加一个显示姓“王”的同学的个数,此处不考虑复姓问题。效果如图:
- 请将字符串数组{ "中国", "美国", "巴西", "澳大利亚", "加拿大" }中的内容反转。然后输出反转后的数组。不能用数组的Reverse()方法。
- 创建一个Person类,属性:姓名、性别、年龄;方法:SayHi() 。再创建一个Employee类继承Person类,扩展属性Salary,重写SayHi方法。
案例:使用WinForm窗体,制作一个简易计算器,默认为“请选择”。要求具有+、-、*、/功能,当用户点击“等于”按钮时,如果输入的为非数字则提示用户。效果如图:
- 编写代码说明什么是方法重载。要求:至少编写3个方法。
//方法名相同参数不同 (方法的重载)
练习1-3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 练习1_3 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //Console.WriteLine("请输入你的考试成绩"); 14 //int score = Convert.ToInt32(Console.ReadLine()); 15 16 //for (int i = 1; i <= 9; i++) 17 //{ 18 // for (int j = 1; j <=9 ; j++) 19 // { 20 // Console.Write("{0}*{1}={2}\t", i, j, i * j); 21 // } 22 // Console.WriteLine(); 23 //} 24 //Console.ReadKey(); 25 26 //定义长度50的数组,随机给数组赋值,并可以让用户输入一个数字n,按一行n个数输出数组 27 //int[] array = new int[50]; Random r = new Random(); r.Next(); 28 //int[] nums = new int[50]; 29 //Random r = new Random(); 30 //for (int i = 0; i < nums.Length; i++) 31 //{ 32 // int rNumber = r.Next(0, 10); 33 // nums[i] = rNumber; 34 //} 35 //Console.WriteLine("请输入一个数字"); 36 //int n = Convert.ToInt32(Console.ReadLine()); 37 //for (int i = 0; i < nums.Length; i++) 38 //{ 39 // Console.Write(nums[i]+"\t"); 40 // if ((i + 1) % n == 0) 41 // { 42 // Console.WriteLine(); 43 // } 44 //} 45 //Console.ReadKey(); 46 47 //编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成小写然后返回(命名规范 骆驼命名) 48 //name s.SubString(0,1) s.SubString(1); 49 //string s = "AAbcd"; 50 //string sNew = ProcessStr(s); 51 //Console.WriteLine(sNew); 52 //Console.ReadKey(); 53 54 int n1 = 10; 55 int n2 = 20; 56 57 //n1 = n1 - n2;//n1=-10 n2=20 58 //n2 = n1 + n2;//n1=-10 n2=10 59 //n1 = n2 - n1;// 60 //int temp = n1; 61 //n1 = n2; 62 //n2 = temp; 63 Change(ref n1, ref n2); 64 Console.WriteLine(n1); 65 Console.WriteLine(n2); 66 Console.ReadKey(); 67 } 68 public static void Change(ref int n1,ref int n2) 69 { 70 int temp = n1; 71 n1 = n2; 72 n2 = temp; 73 } 74 75 76 public static string ProcessStr(string str) 77 { 78 string s = str.Substring(0, 1).ToLower(); 79 return s + str.Substring(1); 80 } 81 82 public static string GetLevel(int score) 83 { 84 string level = null; 85 switch (score / 10) 86 { 87 case 10: 88 case 9: level = "优"; 89 break; 90 case 8: level = "良"; 91 break; 92 case 7: level = "中"; 93 break; 94 case 6: level = "差"; 95 break; 96 default: level = "不及格"; 97 break; 98 } 99 #region if else-if的写法100 //if (score >= 90)101 //{102 // level = "优";103 //}104 //else if (score >= 80)105 //{106 // level = "良";107 //}108 //else if (score >= 70)109 //{110 // level = "中";111 //}112 //else if (score >= 60)113 //{114 // level = "差";115 //}116 //else117 //{118 // level = "不及格";119 //} 120 #endregion121 return level;122 }123 }124 }
练习8-9
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 练习8_9 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //请用户输入一个字符串,计算字符串中的字符个数,并输出。14 //string s = "abcdef";15 //Console.WriteLine(s.Length);16 //Console.ReadKey();17 18 //用方法来实现:计算两个数的最大值。19 //思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:params)。20 21 //Console.WriteLine("请输入第一个数字");22 //int n1 = Convert.ToInt32(Console.ReadLine());23 //Console.WriteLine("请输入第二个数字");24 //int n2 = Convert.ToInt32(Console.ReadLine());25 //int max = GetMax(n1, n2);26 //Console.WriteLine(max);27 int[] nums = { 1, 2, 3, 4, 5 };28 int max = GetMax(1,2,3,4,5,6);29 Console.WriteLine(max);30 Console.ReadKey();31 }32 33 public static int GetMax(params int[] nums)34 {35 int max = nums[0];36 for (int i = 0; i < nums.Length; i++)37 {38 if (nums[i] > max)39 {40 max = nums[i];41 }42 }43 return max;44 }45 46 47 public static int GetMax(int n1, int n2)48 {49 return n1 > n2 ? n1 : n2;50 }51 }52 }
练习10-14
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 练习10_14 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //while (true)14 //{15 // Console.WriteLine("请输入一个数字");16 // int n = Convert.ToInt32(Console.ReadLine());17 // bool b = IsPrime(n);18 // Console.WriteLine(b);19 // Console.ReadKey();20 //}21 //用方法来实现:计算1-100之间的所有质数(素数)的和。22 int sum = GetPrimeSum();23 Console.WriteLine(sum);24 Console.ReadKey();25 }26 27 public static int GetPrimeSum()28 {29 int sum = 0;30 for (int i = 2; i <= 100; i++)31 {32 bool b = true;33 for (int j = 2; j < i; j++)34 {35 if (i % j == 0)36 {37 b = false;38 break;39 }40 }41 if (b)42 {43 sum += i;44 }45 }46 return sum;47 }48 49 50 public static bool IsPrime(int n)51 {52 if (n < 2)53 {54 return false;55 }56 else//>=257 {58 for (int i = 2; i < n; i++)59 {60 if (n % i == 0)61 {62 return false;63 }64 }65 return true;66 }67 }68 public static int GetSum()69 {70 int sum = 0;71 for (int i =2; i < 101; i+=2)72 {73 sum += i;74 }75 return sum;76 }77 }78 }
练习15-18
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 练习15_18 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。 14 //string[] names = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" }; 15 //string max = GetLongest(names); 16 //Console.WriteLine(max); 17 //Console.ReadKey(); 18 19 20 //用方法来实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。 21 //要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。 22 23 // int[] numbers = { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }; 24 // double avg = GetAvg(numbers); 25 // avg = Convert.ToDouble(avg.ToString("0.00")); 26 // Console.WriteLine(avg); 27 //// Console.WriteLine("{0:0.00}", avg); 28 // Console.ReadKey(); 29 30 ////请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。 31 //int[] nums = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }; 32 //Array.Sort(nums);//升序 33 //Array.Reverse(nums);//翻转 34 //for (int i = 0; i < nums.Length - 1; i++) 35 //{ 36 // for (int j = 0; j < nums.Length-1-i; j++) 37 // { 38 // if (nums[j] > nums[j + 1]) 39 // { 40 // int temp = nums[j]; 41 // nums[j] = nums[j + 1]; 42 // nums[j + 1] = temp; 43 // } 44 // } 45 //} 46 47 //for (int i = 0; i < nums.Length; i++) 48 //{ 49 // Console.WriteLine(nums[i]); 50 //} 51 // Console.ReadKey(); 52 53 //为教师编写一个程序,该程序使用一个数组存储30个学生的考试成绩,并给各个数组元素指定一个1-100的随机值,然后计算平均成绩。 54 //int[] nums = new int[30]; 55 //Random r = new Random(); 56 //for (int i = 0; i < nums.Length; i++) 57 //{ 58 // nums[i] = r.Next(1, 101); 59 //} 60 61 62 //double sum = 0; 63 //for (int i = 0; i < nums.Length; i++) 64 //{ 65 // sum += nums[i]; 66 //} 67 //double avg = sum / nums.Length; 68 //avg = Convert.ToDouble(avg.ToString("0.00")); 69 //Console.WriteLine(avg); 70 //Console.ReadKey(); 71 //string[] s=new String; 72 73 74 } 75 76 public static double GetAvg(int[] nums) 77 { 78 double sum = 0; 79 for (int i = 0; i < nums.Length; i++) 80 { 81 sum += nums[i]; 82 } 83 return sum / nums.Length; 84 } 85 86 87 public static string GetLongest(string[] names) 88 { 89 string max = names[0]; 90 for (int i = 0; i < names.Length; i++) 91 { 92 if (names[i].Length > max.Length) 93 { 94 max = names[i]; 95 } 96 } 97 return max; 98 } 99 }100 }
练习19-22
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 练习19_22 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //string str = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"; 14 //int index = str.IndexOf("咳嗽"); 15 //int i = 1; 16 //Console.WriteLine("第1次出现咳嗽的位置是{0}", index); 17 //while (index != -1) 18 //{ 19 // i++; 20 // index = str.IndexOf("咳嗽", index + 1); 21 // if (index == -1) 22 // { 23 // break; 24 // } 25 // Console.WriteLine("第{0}次找到咳嗽的位置是{1}",i,index); 26 //} 27 //Console.ReadKey(); 28 29 30 31 //将字符串" hello world,你 好 世界 ! " 32 //两端空格去掉,并且将其中的所有其他空格都替换成一个空格, 33 //输出结果为:"hello world,你 好 世界 !"。 34 //string s = " hello world,你 好 世界 ! "; 35 //s = s.Trim(); 36 //string[] sNew = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); 37 38 //string strNew = string.Join(" ", sNew); 39 //Console.Write(strNew); 40 //Console.ReadKey(); 41 42 43 44 //存储用户输入的姓名 45 //Listlist = new List (); 46 //while (true) 47 //{ 48 // Console.WriteLine("请输入学员姓名,输入quit退出"); 49 // string name = Console.ReadLine(); 50 // if (name != "quit") 51 // { 52 // list.Add(name); 53 // } 54 // else 55 // { 56 // break; 57 // } 58 //} 59 //int i = 0; 60 //Console.WriteLine("您刚才一共录入了{0}个人的成绩,分别是:",list.Count); 61 //foreach (string item in list) 62 //{ 63 // if (item[0] == '王') 64 // { 65 // i++; 66 // } 67 // Console.WriteLine(item); 68 //} 69 //Console.WriteLine("姓王的同学的个数一共有{0}个",i); 70 //Console.ReadKey(); 71 72 73 74 //请将字符串数组{ "中国", "美国", "巴西", "澳大利亚", "加拿大" }中的内容反转。然后输出反转后的数组。不能用数组的Reverse()方法。 75 76 //string[] names = { "中国", "美国", "巴西", "澳大利亚", "加拿大" }; 77 //for (int i = 0; i < names.Length / 2; i++) 78 //{ 79 // string temp = names[i]; 80 // names[i] = names[names.Length - 1 - i]; 81 // names[names.Length - 1 - i] = temp; 82 //} 83 84 85 //foreach (var item in names) 86 // Console.WriteLine(item); 87 //if(...) 88 //{.....} 89 90 //创建一个Person类,属性:姓名、性别、年龄;方法:SayHi() 。 91 //再创建一个Employee类继承Person类,扩展属性Salary,重写SayHi方法。 92 Console.ReadKey(); 93 } 94 } 95 96 public class Person 97 { 98 public string Name 99 {100 get;101 set;102 }103 104 public char Gender105 {106 get;107 set;108 }109 110 public int Age111 {112 get;113 set;114 }115 116 117 public virtual void SayHi()118 {119 Console.WriteLine("父类打招呼");120 }121 }122 123 124 public class Employee : Person125 {126 public double Salary127 {128 get;129 set;130 }131 132 public override void SayHi()133 {134 Console.WriteLine("子类重写父类");135 }136 }137 138 }
练习25
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 11 namespace 练习2512 {13 public partial class Form1 : Form14 {15 public Form1()16 {17 InitializeComponent();18 }19 20 private void Form1_Load(object sender, EventArgs e)21 {22 comboBox1.SelectedIndex = 0;23 }24 25 private void button1_Click(object sender, EventArgs e)26 {27 try28 {29 int n1 = Convert.ToInt32(textBox1.Text.Trim());30 int n2 = Convert.ToInt32(textBox2.Text.Trim());31 string oper = comboBox1.SelectedItem.ToString();32 switch (oper)33 {34 case "+": label1.Text = (n1 + n2).ToString();35 break;36 case "-": label1.Text = (n1 - n2).ToString();37 break;38 case "*": label1.Text = (n1 * n2).ToString();39 break;40 case "/": label1.Text = (n1 / n2).ToString();41 break;42 default: MessageBox.Show("请选择正确的操作符");43 break;44 }45 }46 catch47 {48 MessageBox.Show("请输入正确的数字");49 }50 }51 }52 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 namespace 练习25 2 { 3 partial class Form1 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// 清理所有正在使用的资源。 12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 ///26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// 29 private void InitializeComponent() 30 { 31 this.textBox1 = new System.Windows.Forms.TextBox(); 32 this.textBox2 = new System.Windows.Forms.TextBox(); 33 this.comboBox1 = new System.Windows.Forms.ComboBox(); 34 this.button1 = new System.Windows.Forms.Button(); 35 this.label1 = new System.Windows.Forms.Label(); 36 this.SuspendLayout(); 37 // 38 // textBox1 39 // 40 this.textBox1.Location = new System.Drawing.Point(37, 88); 41 this.textBox1.Name = "textBox1"; 42 this.textBox1.Size = new System.Drawing.Size(100, 21); 43 this.textBox1.TabIndex = 0; 44 // 45 // textBox2 46 // 47 this.textBox2.Location = new System.Drawing.Point(285, 88); 48 this.textBox2.Name = "textBox2"; 49 this.textBox2.Size = new System.Drawing.Size(100, 21); 50 this.textBox2.TabIndex = 1; 51 // 52 // comboBox1 53 // 54 this.comboBox1.FormattingEnabled = true; 55 this.comboBox1.Items.AddRange(new object[] { 56 "请选择操作符", 57 "+", 58 "-", 59 "*", 60 "/"}); 61 this.comboBox1.Location = new System.Drawing.Point(144, 88); 62 this.comboBox1.Name = "comboBox1"; 63 this.comboBox1.Size = new System.Drawing.Size(121, 20); 64 this.comboBox1.TabIndex = 2; 65 // 66 // button1 67 // 68 this.button1.Location = new System.Drawing.Point(392, 88); 69 this.button1.Name = "button1"; 70 this.button1.Size = new System.Drawing.Size(75, 23); 71 this.button1.TabIndex = 3; 72 this.button1.Text = "="; 73 this.button1.UseVisualStyleBackColor = true; 74 this.button1.Click += new System.EventHandler(this.button1_Click); 75 // 76 // label1 77 // 78 this.label1.AutoSize = true; 79 this.label1.Location = new System.Drawing.Point(489, 95); 80 this.label1.Name = "label1"; 81 this.label1.Size = new System.Drawing.Size(41, 12); 82 this.label1.TabIndex = 4; 83 this.label1.Text = "label1"; 84 // 85 // Form1 86 // 87 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 88 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 89 this.ClientSize = new System.Drawing.Size(762, 433); 90 this.Controls.Add(this.label1); 91 this.Controls.Add(this.button1); 92 this.Controls.Add(this.comboBox1); 93 this.Controls.Add(this.textBox2); 94 this.Controls.Add(this.textBox1); 95 this.Name = "Form1"; 96 this.Text = "Form1"; 97 this.Load += new System.EventHandler(this.Form1_Load); 98 this.ResumeLayout(false); 99 this.PerformLayout();100 101 }102 103 #endregion104 105 private System.Windows.Forms.TextBox textBox1;106 private System.Windows.Forms.TextBox textBox2;107 private System.Windows.Forms.ComboBox comboBox1;108 private System.Windows.Forms.Button button1;109 private System.Windows.Forms.Label label1;110 }111 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace 练习25 8 { 9 static class Program10 {11 ///12 /// 应用程序的主入口点。13 /// 14 [STAThread]15 static void Main()16 {17 Application.EnableVisualStyles();18 Application.SetCompatibleTextRenderingDefault(false);19 Application.Run(new Form1());20 }21 }22 }
使用GDI绘制简单的图形
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 11 namespace 使用GDI绘制简单的图形12 {13 public partial class Form1 : Form14 {15 public Form1()16 {17 InitializeComponent();18 }19 20 private void Form1_Load(object sender, EventArgs e)21 {22 //一根笔 颜色 一张纸 两点 绘制直线的对象23 }24 25 private void button1_Click(object sender, EventArgs e)26 {27 //创建GDI对象28 Graphics g = this.CreateGraphics();// new Graphics();29 //创建画笔对象30 Pen pen = new Pen(Brushes.Red);31 //创建两个点32 Point p1 = new Point(30, 50);33 Point p2 = new Point(250, 250);34 g.DrawLine(pen, p1, p2);35 36 }37 int i = 0;38 private void Form1_Paint(object sender, PaintEventArgs e)39 {40 i++;41 label1.Text = i.ToString();42 Graphics g = this.CreateGraphics();// new Graphics();43 //创建画笔对象44 Pen pen = new Pen(Brushes.Red);45 //创建两个点46 Point p1 = new Point(30, 50);47 Point p2 = new Point(250, 250);48 g.DrawLine(pen, p1, p2);49 50 }51 52 private void button2_Click(object sender, EventArgs e)53 {54 Graphics g = this.CreateGraphics();55 Pen pen=new Pen(Brushes.Yellow);56 Size size=new System.Drawing.Size(80,80);57 Rectangle rec=new Rectangle(new Point(50,50),size);58 g.DrawRectangle(pen,rec);59 }60 61 private void button3_Click(object sender, EventArgs e)62 {63 Graphics g = this.CreateGraphics();64 Pen pen=new Pen(Brushes.Blue);65 Size size=new System.Drawing.Size(180,180);66 Rectangle rec=new Rectangle(new Point(150,150),size);67 g.DrawPie(pen, rec, 60, 60);68 }69 70 private void button4_Click(object sender, EventArgs e)71 {72 Graphics g = this.CreateGraphics();73 g.DrawString("老赵是最帅的", new Font("宋体", 20, FontStyle.Underline), Brushes.Black, new Point(300, 300));74 }75 }76 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 namespace 使用GDI绘制简单的图形 2 { 3 partial class Form1 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// 清理所有正在使用的资源。 12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 ///26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// 29 private void InitializeComponent() 30 { 31 this.button1 = new System.Windows.Forms.Button(); 32 this.label1 = new System.Windows.Forms.Label(); 33 this.button2 = new System.Windows.Forms.Button(); 34 this.button3 = new System.Windows.Forms.Button(); 35 this.button4 = new System.Windows.Forms.Button(); 36 this.SuspendLayout(); 37 // 38 // button1 39 // 40 this.button1.Location = new System.Drawing.Point(367, 12); 41 this.button1.Name = "button1"; 42 this.button1.Size = new System.Drawing.Size(145, 23); 43 this.button1.TabIndex = 0; 44 this.button1.Text = "绘制一条直线"; 45 this.button1.UseVisualStyleBackColor = true; 46 this.button1.Click += new System.EventHandler(this.button1_Click); 47 // 48 // label1 49 // 50 this.label1.AutoSize = true; 51 this.label1.Location = new System.Drawing.Point(418, 118); 52 this.label1.Name = "label1"; 53 this.label1.Size = new System.Drawing.Size(41, 12); 54 this.label1.TabIndex = 1; 55 this.label1.Text = "label1"; 56 // 57 // button2 58 // 59 this.button2.Location = new System.Drawing.Point(367, 170); 60 this.button2.Name = "button2"; 61 this.button2.Size = new System.Drawing.Size(145, 23); 62 this.button2.TabIndex = 2; 63 this.button2.Text = "绘制一个矩形"; 64 this.button2.UseVisualStyleBackColor = true; 65 this.button2.Click += new System.EventHandler(this.button2_Click); 66 // 67 // button3 68 // 69 this.button3.Location = new System.Drawing.Point(367, 240); 70 this.button3.Name = "button3"; 71 this.button3.Size = new System.Drawing.Size(145, 23); 72 this.button3.TabIndex = 3; 73 this.button3.Text = "绘制一个扇形"; 74 this.button3.UseVisualStyleBackColor = true; 75 this.button3.Click += new System.EventHandler(this.button3_Click); 76 // 77 // button4 78 // 79 this.button4.Location = new System.Drawing.Point(367, 139); 80 this.button4.Name = "button4"; 81 this.button4.Size = new System.Drawing.Size(145, 23); 82 this.button4.TabIndex = 4; 83 this.button4.Text = "绘制一个文本"; 84 this.button4.UseVisualStyleBackColor = true; 85 this.button4.Click += new System.EventHandler(this.button4_Click); 86 // 87 // Form1 88 // 89 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 90 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 91 this.ClientSize = new System.Drawing.Size(564, 417); 92 this.Controls.Add(this.button4); 93 this.Controls.Add(this.button3); 94 this.Controls.Add(this.button2); 95 this.Controls.Add(this.label1); 96 this.Controls.Add(this.button1); 97 this.Name = "Form1"; 98 this.Text = "Form1"; 99 this.Load += new System.EventHandler(this.Form1_Load);100 this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);101 this.ResumeLayout(false);102 this.PerformLayout();103 104 }105 106 #endregion107 108 private System.Windows.Forms.Button button1;109 private System.Windows.Forms.Label label1;110 private System.Windows.Forms.Button button2;111 private System.Windows.Forms.Button button3;112 private System.Windows.Forms.Button button4;113 }114 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace 使用GDI绘制简单的图形 8 { 9 static class Program10 {11 ///12 /// 应用程序的主入口点。13 /// 14 [STAThread]15 static void Main()16 {17 Application.EnableVisualStyles();18 Application.SetCompatibleTextRenderingDefault(false);19 Application.Run(new Form1());20 }21 }22 }
使用GDI绘制验证码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 11 namespace 使用GDI绘制验证码12 {13 public partial class Form1 : Form14 {15 public Form1()16 {17 InitializeComponent();18 }19 20 21 ///22 /// 点击更换验证码23 /// 24 /// 25 /// 26 private void pictureBox1_Click(object sender, EventArgs e)27 {28 Random r = new Random();29 string str = null;30 for (int i = 0; i < 5; i++)31 {32 int rNumber = r.Next(0, 10);33 str += rNumber;34 }35 // MessageBox.Show(str);36 //创建GDI对象37 Bitmap bmp = new Bitmap(150, 40);38 Graphics g = Graphics.FromImage(bmp);39 40 for (int i = 0; i < 5; i++)41 {42 Point p = new Point(i * 20, 0);43 string[] fonts = { "微软雅黑", "宋体", "黑体", "隶书", "仿宋" };44 Color[] colors = { Color.Yellow, Color.Blue, Color.Black, Color.Red, Color.Green };45 g.DrawString(str[i].ToString(), new Font(fonts[r.Next(0, 5)], 20, FontStyle.Bold), new SolidBrush(colors[r.Next(0, 5)]), p);46 }47 48 for (int i = 0; i < 20; i++)49 {50 Point p1=new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));51 Point p2=new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));52 g.DrawLine(new Pen(Brushes.Green), p1, p2);53 }54 55 for (int i = 0; i < 500; i++)56 {57 Point p=new Point(r.Next(0,bmp.Width),r.Next(0,bmp.Height));58 bmp.SetPixel(p.X, p.Y, Color.Black);59 }60 61 62 //将图片镶嵌到PictureBox中63 pictureBox1.Image = bmp;64 }65 }66 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 namespace 使用GDI绘制验证码 2 { 3 partial class Form1 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// 清理所有正在使用的资源。12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。14 protected override void Dispose(bool disposing)15 {16 if (disposing && (components != null))17 {18 components.Dispose();19 }20 base.Dispose(disposing);21 }22 23 #region Windows 窗体设计器生成的代码24 25 ///26 /// 设计器支持所需的方法 - 不要27 /// 使用代码编辑器修改此方法的内容。28 /// 29 private void InitializeComponent()30 {31 this.pictureBox1 = new System.Windows.Forms.PictureBox();32 ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();33 this.SuspendLayout();34 // 35 // pictureBox136 // 37 this.pictureBox1.Location = new System.Drawing.Point(140, 41);38 this.pictureBox1.Name = "pictureBox1";39 this.pictureBox1.Size = new System.Drawing.Size(439, 314);40 this.pictureBox1.TabIndex = 0;41 this.pictureBox1.TabStop = false;42 this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);43 // 44 // Form145 // 46 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);47 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;48 this.ClientSize = new System.Drawing.Size(716, 448);49 this.Controls.Add(this.pictureBox1);50 this.Name = "Form1";51 this.Text = "Form1";52 ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();53 this.ResumeLayout(false);54 55 }56 57 #endregion58 59 private System.Windows.Forms.PictureBox pictureBox1;60 }61 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace 使用GDI绘制验证码 8 { 9 static class Program10 {11 ///12 /// 应用程序的主入口点。13 /// 14 [STAThread]15 static void Main()16 {17 Application.EnableVisualStyles();18 Application.SetCompatibleTextRenderingDefault(false);19 Application.Run(new Form1());20 }21 }22 }