任务49:148-数组的定义和初始化
数组
任务50:149-使用for循环,while循环和foreach来遍历数组
数组的遍历
- foreach
任务51:150-字符串的处理int[] array = { 1, 2, 4, 8 };foreach (var i in array){Console.WriteLine(i);}
字符串的处理
split方法string str = "asdfghj";for (int i = 0; i < str.Length; i++) // .length 访问长度{Console.Write(str[i]); // [i] 访问第i+1个字符}
任务52:151-练习1和2 练习for循环string str = "fdasdfghjasdsaddd";string[] strArray = str.Split('a'); //把字符串拆分成数组
练习1:找出水仙花数
见“算法”栏目(略)
练习2:3个可乐瓶换一瓶可乐,x瓶可乐一共可以喝多少瓶
任务53:152-练习3和4 循环结构练习和字符操作int start = Convert.ToInt32(Console.ReadLine());int total = start;int empty = start;while (empty > 1){if (empty % 3 == 0){start = empty / 3;total = total + start;empty = start;}else if (empty % 3 == 1){start = (empty - 1) / 3;total = total + start;empty = start + 1;}else if (empty % 3 == 2){start = (empty + 1) / 3;total = total + start;empty = start - 1;}}Console.WriteLine(total);
练习3:用户猜1-50的数字
练习4:对输入的字符串进行加密Random random = new Random();int x = random.Next(1, 51);Console.WriteLine("我想好了,你猜吧(范围1-50)");int y;while (true){y = Convert.ToInt32(Console.ReadLine());if (y > x){Console.WriteLine("太大了,继续猜");}else if (y < x){Console.WriteLine("太小了,继续猜");}else{break;}}Console.WriteLine("猜对了,答案就是{0}", x);
任务54:153-练习5 对数组进行排序,Array.Sort和冒泡排序算法string input = Console.ReadLine();string output = "";for (int i = 0; i < input.Length; i++){if (input[i] >= 'a' && input[i] <= 'z'){int before = input[i];before = before + 3;char after = (char)before; //显示转换if (after > 'z') //将xyz转换为abc{after = (char)(after - 26);}output = output + after;}else if (input[i] >= 'A' && input[i] <= 'Z'){int before = input[i];before = before + 3;char after = (char)before; //显示转换if (after > 'Z'){after = (char)(after - 26);}output = output + after;}else{output = output + input[i];}}Console.WriteLine(output);
练习5:对用户输入的一串数字进行排序
任务55:154-练习6和7 循环练习string str = Console.ReadLine(); //要求用户输入一组整数,以空格分隔string[] strArray = str.Split(' '); //将用户输入的一串字符转变为字符组int[] numArray = new int[strArray.Length]; //将字符组转变为数组for (int i = 0; i < strArray.Length; i++){int temp = Convert.ToInt32(strArray[i]);numArray[i] = temp;}Array.Sort(numArray); //使用CLR给我们提供的方法进行排序(这个方法其实使用了快速排序算法)for (int i = 0; i < numArray.Length; i++){Console.Write(numArray[i] + " ");}
练习6:用户输入n,输出n次循环i=(i+1)*2的结果,i初始值为1
练习7:略double x = 1;int n = Convert.ToInt32(Console.ReadLine());for (int i = 1; i <= n; i++){x = (x + 1) * 2;}Console.WriteLine(x);
任务56:155-练习8 循环练习
练习8:用户输入一个整数,将整数依序插入到从小到大排序的数组中
比如{0,2,4,6,8,10,12,14,16,18,20}
int[] OldArray = new int[] { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };int[] NewArray = new int[OldArray.Length + 1];Console.WriteLine("Please input integer");int x = Convert.ToInt32(Console.ReadLine());bool Trigger = true;if (x <= OldArray[0]) //检测x是否为最小值{NewArray[0] = x;Trigger = false;}else if (x >= OldArray[OldArray.Length - 1]) //检测x是否为最大值{NewArray[NewArray.Length - 1] = x;Trigger = false;}for (int i = 0; i <= OldArray.Length - 1; i++){//检测x是否在某两个数之间,运行这段代码的前提是x不为最大值或最小值if (Trigger == true && x >= OldArray[i] && x <= OldArray[i + 1]){NewArray[i] = OldArray[i];NewArray[i+1] = x;}else if (x > OldArray[i]){NewArray[i] = OldArray[i];}else if (x < OldArray[i]){NewArray[i + 1] = OldArray[i];}}for (int i = 0; i <= NewArray.Length - 1; i++){Console.Write(NewArray[i] + " ");}
别人写的:
int[] num = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };int x = Convert.ToInt32(Console.ReadLine());int[] num2 = new int[num.Length + 1];int i = 0, j = 0;for (i = 0; i < num.Length && num[i] < x; ++i){num2[j++] = num[i];}num2[j++] = x;for (; i < num.Length; ++i){num2[j++] = num[i];}Console.WriteLine("原数组:");for (i = 0; i < num.Length; ++i){Console.Write(num[i] + " ");}Console.WriteLine();Console.WriteLine("插入数: " + x);Console.WriteLine("新数组:");for (i = 0; i < num2.Length; ++i){Console.Write(num2[i] + " ");}Console.WriteLine();
任务57:156-练习9和10 循环练习和字符串与字符的处理
练习9:用户输入一个整数,输出该数被256,43,1拆分的结果
Console.WriteLine("Please input an integer");int x = Convert.ToInt32(Console.ReadLine());int a = x / 256;int b = (x - 256 * a) / 43;int c = (x - 256 * a - 43 * b);Console.WriteLine("{0}={1}*256+{2}*43+{3}*1", x, a, b, c);
练习10:输入一个字符串,判断是否是C#的合法标识符
(略)
任务58:157-练习11和12 循环练习和字符串处理
练习11:判断用户输入的字符串是否为回文
Console.WriteLine("请输入一个回文");string input = Console.ReadLine();bool temp = true;for (int i = 0; i <= input.Length - 1; i++){if (input[i] == input[input.Length - 1 - i]){}else{Console.WriteLine("你输入的不是回文");temp = false;break;}}if (temp == true){Console.WriteLine("你输入的是回文");}
练习12:校验一串密码是否符合密码安全规则(略)

