5.4穷举,迭代
1.namespace 三角形1 { class Program { static void Main(string[] args) { //循环打印行 for (int i = 1 ; i < 6; i++) { string a = "";//内容 //循环填充每行的内容 for (int j = 1; j <= i; j++) { a += "★"; } Console.WriteLine(a); } Console.ReadLine(); } } } 2.namespace 三角形2 { class Program { static void Main(string[] args) { for (int i = 1; i < 6; i++) { string a = ""; for (int j = 5; j >= i; j--) { a += "★"; } Console.WriteLine(a); } Console.ReadLine(); } } } 3.namespace 三角形3 { class Program { static void Main(string[] args) { for (int i = 1; i < 6; i++) { //内容拼接 string a = ""; string b = ""; for (int j = 5; j > i; j--) { a += " "; } for (int k = 1; k <= i; k++) { b += "★"; } Console.WriteLine(a + b); } Console.ReadLine(); 4.amespace 方形 { class Program { static void Main(string[] args) { //打印方形,每行打印10个“A”,打印10行, //使用循环嵌套,不允许定义内容为“AAAAAAAAAA”的变量; for (int i = 0; i < 10; i++) { string a = ""; for (int j = 0; j < 10; j++) { a += "A"; } Console.WriteLine(a); } Console.ReadLine(); } } } 5.namespace 去逗号 { class Program { static void Main(string[] args) { //打印1-100所有的数,并在100的后面不允许出现逗号 for (int i = 1; i <= 100; i++) { if (i < 100) { Console.Write(i + ","); } else { Console.Write(i); } } Console.ReadLine(); } } } //6.、用户输入姓名 Console.Write("请输入您的姓名:"); string name = Console.ReadLine(); //2、循环打印年龄 for (int i = 1; i < 19; i++) { //3、打印年龄的过程中增加判断 if (i == 6) { Console.WriteLine("我叫" + name + ",我今年" + i + "岁了,我上小学了!"); } else if (i == 11) { Console.WriteLine("我叫" + name + ",我今年" + i + "岁了,我上初中了!"); } else if (i == 15) { Console.WriteLine("我叫" + name + ",我今年" + i + "岁了,我上高中了!"); } else if (i == 18) { Console.WriteLine("我叫" + name + ",我今年" + i + "岁了,我上大学了!我成年了!"); } else { Console.WriteLine("我叫" + name + ",我今年" + i + "岁了!"); } } Console.ReadLine(); } } }