三月12号的 问题特别多
今天讲的内容有些多主要是类的复习以及数组的练习,都是一些比较复杂的题型的,需要多加练习才可以的
首先是关于截取的练习输入身份证号码截取生日
//输入身份证号接受生日 Console.Write("请输入你的身份证号"); string a = Console.ReadLine(); if (a.Length== 18) { string c = a.Substring(6,4); string d = a.Substring(11, 1); string e = a.Substring(12,2); Console.WriteLine(""+c+"年"+d+"月"+e+"日"); } else { Console.WriteLine("有误"); } Console.ReadLine();
//邮箱格式 //1.有且只有一个@, //2.不能以@开头 //@之后至少有一个. //@和.不能靠在一起 //不能以.结尾 Console.Write("请输入你的邮箱"); string mail = Console.ReadLine(); bool a = mail.Contains("@");//判断是否包含@ if (a == true) { int b = mail.IndexOf("@"); int c = mail.LastIndexOf("@");//判断只有一个@ if (b == c) { if (b != 0)//不以@开头,索引第一位为0 { string mail1 = mail.Substring(b);//从@开始截取 if(mail1.Contains(".")) //从@截取的mail1中是不是含有. { int d = mail1.IndexOf(".");//mail1中.的索引 if (d != 1)//不是和@紧挨的 { int e = mail.LastIndexOf(".");//最后一个.的索引号 if(e!=mail.Length-1)//长度减一等于索引 { Console.WriteLine("输入正确"); } else { Console.WriteLine("输入有误"); } } else { Console.WriteLine("输入有误"); } } else { Console.WriteLine("输入有误"); } } else { Console.WriteLine("输入有误"); } } else { Console.WriteLine("输入有误"); } } else { Console.WriteLine("输入有误"); } Console.ReadLine();
输出结果的展示
关于验证码的第一个练习题,错了就是错了的形式
string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random ran = new Random(); string biao = ""; for (int i=0;i<4 ;i++ ) { biao += s.Substring(ran.Next(s.Length), 1); } Console.WriteLine(biao); Console.Write("请输入验证码"); string shu = Console.ReadLine(); if (biao.ToLower() == shu.ToLower()) { Console.WriteLine("输入正确"); } else { Console.WriteLine("输入错误"); } Console.ReadLine();
一般的验证码都是错了清屏再重新输入的以下就是此案例的补充
//验证码输入错误后重新输入 string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random ran = new Random(); for (; ; ) { string biao = ""; for (int i = 1; i <= 4; i++) { biao += s.Substring(ran.Next (s.Length),1); } Console.WriteLine(biao); Console.Write("请输入验证码"); string shu = Console.ReadLine(); if (shu.ToLower() == biao.ToLower()) { Console.WriteLine("输入正确"); break; } else { Console.Clear(); Console.WriteLine("输入有误重新输入"); } } Console.ReadLine();
数组:具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。
定义数组
int[] 变量名 = new int [n];
string[] myStringArray = new string[6];
int[] myArray = new int[] {1, 3, 5, 7, 9};
取值:int a = myArray[0];
一维数组的两种表达形式
//int[] shuzu = new int[5];
////索引从0开始
////赋值
//shuzu[0] = 1;
//shuzu[1] = 2;
//shuzu[2] = 3;
//shuzu[3] = 4;
//shuzu[4] = 5;
//shuzu[5] = 6;//不可以超出上限
//Console.WriteLine(shuzu[3]);
//第二种定义
//int[] shuzu = new int[] { 1, 2, 3, 4, 5 };
//Console.WriteLine(shuzu[4]);
//从控制台输入10个人的年龄放入数组
//将十个人的年龄求总和
int[] age = new int[10];
int sum = 0;
for (int i = 0; i < 10; i++)
{
Console.Write("第"+(i+1)+"个人的年龄是");
age[i] = int.Parse(Console.ReadLine());
sum+=age[i];
}
Console.WriteLine(sum);
Console.ReadLine();
注意【】里面的是索引,不能超出上限
//输入人名放进数组,输出第5个的姓名 //输出第5个首先要求比5要大才可以 Console.Write("请输入班级的人数"); int a = int.Parse(Console.ReadLine()); string[] name = new string[a]; for (int i = 0; i < a; i++) { Console.Write("第"+(i+1)+"个人的姓名是"); name[i] = Console.ReadLine(); } Console.WriteLine(name[4]); Console.ReadLine();
Console.Write("请输入班级的人数"); int a = int.Parse(Console.ReadLine()); double[] b = new double[a]; double sum = 0; for (int i=0;i<a;i++ ) { Console.Write("第"+(i+1)+"个人的成绩是:"); b[i] = double.Parse(Console.ReadLine()); sum += b[i]; } Console.WriteLine(sum); Console.WriteLine("平均分为"+sum/a); Console.ReadLine();
//二维数组
//int[,] shuzu = new int[3, 4];
//int[,] shuzu = new int[,] {
//{1,2,3,4},
//{5,6,7,8},
//{6,7,8,9}
//};
string[,] shuzu = new string[,]{ {" ","■","■","■","■","■"," "}, {" "," "," ","■"," "," "," "}, {" "," "," ","■"," "," "," "}, {" ","■","■","■","■","■"," "}, {" "," "," ","■"," "," "," "}, {" "," "," ","■"," "," "," "}, {"■","■","■","■","■","■","■"} }; for (int i=0; i < 7; i++) { for (int j = 0; j < 7; j++) { Console.Write(shuzu[i,j]); } Console.WriteLine(); } Console.ReadLine();
//输入班级人数,将每个人的 //语文,数学,英语成绩输入二维数组 Console.Write("请输入班级人数:"); int a = int.Parse(Console.ReadLine()); double[,] shuzu = new double[a, 3]; for (int i = 0; i < a; i++) { for (int j = 0; j < 3; j++) { if (j == 0) { Console.Write("请输入第" + (i + 1) + "个人的语文成绩:"); } if (j == 1) { Console.Write("请输入第" + (i + 1) + "个人的数学成绩:"); } if (j == 2) { Console.Write("请输入第" + (i + 1) + "个人的英语成绩:"); } shuzu[i, j] = double.Parse(Console.ReadLine()); } } Console.ReadLine();
以上是自己的练习