1. while循环、do...while循环、for循环、foreach循环(循环结构)
using System;
using System.Runtime.InteropServices;namespace test_1
{class Program{static void Main(string[] args){//循环结构#region while循环/** 【语法】*while(条件)*{* 循环语句;* break; 跳出循环* continue; 跳过本次循环*}*//*//【例题】输出5遍 Hello Worldint index = 0;while (index < 5){Console.WriteLine("Hello World");index++;}//【练习】//1.计算100 - 10000之间有多少个数,其各位数字之和等于7,例如:241, 2 + 4 + 1 = 7int count1 = 0;int index1 = 101;while (index1 < 10000){//求各位数字int a = index1 % 10; //求个位数字int b = (index1 / 10) % 10; //求十位数字int c = (index1 / 100) % 10; //求百位数字int d = (index1 / 1000) % 10; //求千位数字int s = a + b + c + d;if ( s == 7){count1++;}index1++;}Console.WriteLine(count1);//2.求1-100之间的所有偶数int index2 = 1;while (index2 < 100){if (index2 % 2 == 0){Console.WriteLine(index2);}index2++;}//3.求1+2+...+100的和int count3 = 0;int index3 = 1;while (index3<101){count3 += index3;index3++;}Console.WriteLine(count3);//4.用while和break实现,一旦输入数字小于0,就跳出循环while (true){Console.WriteLine("请输入一个数字:");int index4 = int.Parse(Console.ReadLine());if (index4 < 0)break;}//5.用while和continue实现,输出的是数字 while (true){Console.WriteLine("请输入一个数字:");char index5 = char.Parse(Console.ReadLine());if(index5 < 48 || index5 > 57)continue;Console.WriteLine("您输入的数字是:" + (index5 - 48));}*/#endregionConsole.WriteLine("--------------------------------分割线--------------------------------------");#region do...while循环/** 语法* do{* 循环语句;* }* while(条件表达式)* *//*//【例题】印数字 0 - 9int a1 = 0;do {Console.WriteLine(a1);a1++;} while (a1 < 10);//【练习】//sum = 1 + 2 + 3 + ... + N,求sum不超过10000的最大整数值和项值Nint a2 = 1;int sum = 1;do{sum = sum + a2;a2++;} while (sum <=10000);Console.WriteLine(sum-a2);Console.WriteLine(a2-1);*/#endregionConsole.WriteLine("--------------------------------分割线--------------------------------------");#region for循环/**语法*for (循环变量的初始化;循环条件;循环增量) //循环三要素*{* 循环体;* }* *//*//【例题】输出数字0 - 9for (int i = 0; i < 10; i++){Console.WriteLine(i);}//【练习】//阶乘Console.WriteLine("请输入一个整数");int n = int.Parse(Console.ReadLine());int sum1 = 1;for (int i = 1; i <= n; i++){sum1 = sum1 * i;}Console.WriteLine(sum1);//用For循环输出1-100之间不是7的倍数且各位数字都不能为7的数字for (int i = 1; i < 100; i++){if(i % 7 == 0||i % 10 == 7 || i / 10 == 7)continue;else{Console.WriteLine(i);}}//打印下图/**1*12*123*...* 123..n* *//*Console.WriteLine("请输入一个整数:");int a3 = int.Parse(Console.ReadLine());int sum2 = 0;for (int i = 1; i <= a3; i++){sum2 = sum2 + i;Console.WriteLine(sum2);sum2 = sum2 * 10;}//打印九九乘法口诀表--循环嵌套(双层循环)Console.WriteLine("打印九九乘法口诀表:");for (int i = 1; i <= 9; i++)//外层循环表示行数{for (int j = 1; j <= i; j++)//内层循环表示列数{Console.Write($"{j}X{i}={i*j,2}"); --------------------------------输出不换行/输出数字自定义占位数Console.Write("\t");//--------------------------------------------使用转义字符,一个Tab键的长度,4个长度}Console.WriteLine();}*/#endregionConsole.WriteLine("--------------------------------分割线--------------------------------------");#region foreach循环/*//var number = "1"; var是模糊类型//foreach遍历int[] array = {1, 2, 3, 4, 5, 6};foreach (int item in array){Console.WriteLine(item);}*/#endregion//for 和 foreach对比//foreach 不能边遍历边修改数组元素,不能获取数组元素下标,不能修改数组元素}}
}
2.作业
using System;namespace Day05_3
{class Program{static void Main(string[] args){#region 第一题(三层循环)/*//已知abc+cba = 1333,其中a,b,c均为?位数,编程求出满?条件的a,b,c所有组合。for (int i = 1; i < 10; i++){for (int j = 0; j < 10; j++){for (int k = 1; k < 10; k++){if(i * 100 +j * 10 + k + i + j * 10 + k * 100 == 1333)Console.WriteLine("a={0},b={1},c={2}",i,j,k);}} }*/#endregion#region 第二题辗转相除法(欧几里得算法)/*//输?两个数,求最?公约数和最?公倍数//辗转相除法:又叫欧几里得算法,用来求两个正整数最大公约数.//拓展:扩展欧几里得算法,可用于RSA加密等领域。Console.WriteLine("请输入两个整数:");int num1 = int.Parse(Console.ReadLine());int num2 = int.Parse(Console.ReadLine());int max, min,x,y;if (num1 > num2){max = num1;min = num2;}else{max = num2;min = num1;}while (min != 0){x = max % min;y = min;max = y;min = x;}Console.WriteLine("最大公约数是:" + max );Console.WriteLine("最小公倍数是:" + num1 * num2 / max);*/#endregion#region 第三题(百钱买百鸡) ---------------------- ---------------------结果有问题/*//编?个程序,解决百钱买百鸡问题。某?有100元钱,要买100只鸡。公鸡5元钱?只,?鸡3元钱//?只,?鸡?元钱3只。问可买到公鸡,?鸡,?鸡各为多少只。for (int i = 0; i < 21; i++){for (int j = 0; j < (float)((100 - i*5)/3 ); j++){if(i * 5 + j * 3 + (float)((100 - i - j) /3 )== 100)Console.WriteLine("公鸡:"+ i +"母鸡:" + j + "小鸡:" + (100-i-j));}}*/#endregion#region 第四题(猴子吃桃)/*//猴?第?天摘下若?个桃?,每天都吃了前?天剩下的?半零?个,到第10天早上//想再吃的时候,就剩下?个桃?. 求第?天共摘多少个桃?。int sum = 1;for (int i = 1; i < 10; i++){sum = (sum + 1) * 2;}Console.WriteLine(sum);*/#endregion#region 第五题(求分数之和)/*//编?个程序,?while循环语句来计算1+1/2+2/3+3/4+...+99/100之和int i = 1;float num = 1;float sum = 1;while (i != 101){sum = sum + num / (num + 1);num++;i++;}Console.WriteLine(sum);*/#endregion#region 第六题(三次登录)/*//编?个程序,要求使?while语句,输??户名和密码,实现?户登录程序的功能,?多允许输?//三次,超过三次不允许登录int num = 1;while (num != 4){Console.WriteLine("请输入账号:");string username = Console.ReadLine();Console.WriteLine("请输入密码:");string password = Console.ReadLine();if (username.Equals("admin") && password.Equals("123456")){Console.WriteLine("登录成功");break;}else{Console.WriteLine("输入错误");}num++;}*/#endregion#region 第七题 --------------------------------------------对题目有疑问/*//有关系式11+22+33+...+kk<2000,编?个程序,求出满?此关系式的k的最?值int sum = 0;int i = 1;while(sum < 2000){i++;if (i < 10){sum = sum + i * 11;}else{sum = sum + i * 100 + i;}}Console.WriteLine(i-1);*/#endregion#region 第八题/*//编?个程序,定义?个有10个元素的数组,使?for循环语句,从键盘上输?10名同学的数学成//绩,分别求出最?分和最低分,并且求出10名同学的数学平均成绩。int[] score = new int[10];Console.WriteLine("请输入10名同学成绩:");int sum = 0;for (int i = 0; i < 10; i++){score[i] = int.Parse(Console.ReadLine());sum = sum + score[i];}int max = score[0];int min = score[0];float avg =(float) (sum / 10);for (int i = 1; i < 10; i++){if (score[i] > max){max = score[i];}if (score[i] < min){min = score[i];}}Console.WriteLine("最高成绩:" + max);Console.WriteLine("最低成绩:" + min);Console.WriteLine("平均成绩:" + avg);*/#endregion#region 第九题(阶乘)/*//编?个程序,利?for循环语句,求出1!+2!+3!+...+10!的和int sum = 0;int x = 1;for (int i = 1; i < 11; i++){x = x * i;sum += x;}Console.WriteLine(sum);*/#endregion#region 第十题 -----------------------------------------------同百钱买百鸡问题/*//编?个程序,把?张1元的钞票换成5分,2分和1分的硬币,要求每种?少?枚,并且所换硬币数不//超过30枚。请问,有哪?种换法?int count = 0;for (int i = 1; i < 18; i++){for (int j = 1; j < (100 - 5 * i) / 2 - 1; j++){int k = (100 - 5 * i - 2 * j) / 1;if (i+j+k<=30){count++;}}}Console.WriteLine(count);*/#endregion#region 第十一题 ----------------------------------------------同百钱买百鸡问题/*//编?个程序,把?张?值100元的钞票换成5元,1元和5??值的钞票,要求100元换以上的零钱//100张,且要求每种不少于?张。请问,有哪?种换法?int count = 0;for (int i = 1; i < 19; i++){for (int j = 0; j < 100 - i*5 - 1 ; j++){if (i * 5 + j * 3 + (100 - i - j) / 2 == 100)count++;}}Console.WriteLine(count);*/#endregion#region 第十二题/*//编?个程序,定义?个有10个元素的?维数组a,在键盘上输?时没有??次序,但是存?数组时要按//由?到?的顺序存放。例如,输?第1个数1时,存?a[0];假如第2个数是5,则数存?a[1];假如第//3个数是4,那么把前?输?的5向后?移动到a[2],把4插?到a[1]的位置上,这样使得每输??个数,//保持从?到?的顺序排列int[] array = new int[10];array[0] = int.Parse(Console.ReadLine());for (int i = 1; i < 10; i++){int x = int.Parse(Console.ReadLine());if (x <= array[0]){for (int j = i; j > 0; j--){array[j] = array[j - 1];}array[0] = x;}else if (x > array[i-1]){array[i] = x;}else{for (int j = 0; j < i; j++){if (x < array[j]){for (int k = i; k > j ; k--){array[k] = array[k - 1];}array[j] = x;break;}}}}Console.WriteLine("结果为:");for (int l = 0; l< 10; l++){Console.Write(array[l] + " ");}*/#endregion#region 第十三题(打印菱形)/*int n = int.Parse(Console.ReadLine());//打印空心菱形for (int i = 1; i <= n; i++){for (int j = 2 * n - 1; j >= 1; j--){if (j == n - i + 1){Console.Write("*");}else if (j == n + i - 1){Console.Write("*");}else{Console.Write(" ");}}Console.WriteLine();}for (int i = 1; i <= n-1; i++){for (int j = 2 * n - 1; j >= 1; j--){if (j == i + 1){Console.Write("*");}else if (j == 2 * n - 1 - i ){Console.Write("*");}else{Console.Write(" ");}}Console.WriteLine();}//打印实心菱形for (int i = 1; i <= n; i++){for (int j = n-i; j >= 1; j--){Console.Write(" ");}for (int j = 1; j <= i ; j++){Console.Write("* ");}Console.WriteLine();}for (int i = 1; i <= n-1; i++){for (int j = 1; j <= i ; j++){Console.Write(" ");}for (int j = 1; j <= n - i; j++){Console.Write("* ");}Console.WriteLine();}*/#endregion}}
}