首页 > 代码库 > 2017-2-24 C#基础 for循环的嵌套
2017-2-24 C#基础 for循环的嵌套
用几个练习题演示一下for循环的嵌套
1、打印以下图形
★
★★
★★★
★★★★
★★★★★
namespace _2017_2_24_for循环的嵌套 { class Program { static void Main(string[] args) { Console.WriteLine("请输入一个数字:"); int a=Convert.ToInt32(Console.ReadLine()); for (int i = 1; i <= a;i++ ) { for (int b = 1; b <= i;b++ ) { Console.Write("★"); } Console.Write("\n"); } Console.ReadLine(); } } }
★★★★★
★★★★
★★★
★★
★
namespace _2017_2_24_for循环的嵌套_画星星2 { class Program { static void Main(string[] args) { Console.WriteLine("请输入数字"); int a = Convert.ToInt32(Console.ReadLine()); for (int b = 1; b <= a;b++ ) { for (int c =a; c >= b;c-- ) { Console.Write("★"); } Console.Write("\n"); } Console.ReadLine(); } } }
○○○○★
○○○★★
○○★★★
○★★★★
★★★★★
namespace _2017_2_24_for循环的嵌套__画星星3 { class Program { static void Main(string[] args) { Console.WriteLine("请输入一个数字"); int count = Convert.ToInt32(Console.ReadLine()); String b = " "; String c = "※"; for (int d = 1; d <= count; d++) { for (int e = count - 1; e >= d; e--) { Console.Write(b); } Console.Write(c); for (int x = 0; x < d-1; x++) { Console.Write(c); } Console.Write("\n"); } Console.ReadLine(); } } }
★★★★★
★★★★
★★★
★★
★
namespace _2017_2_24_for_循环_的嵌套__画星星4 { class Program { static void Main(string[] args) { Console.WriteLine("请输入一个数字:"); int a=Convert.ToInt32( Console.ReadLine()); String b=" ";String c="☆"; for (int d = 1; d <= a; d++) { for (int e = 1; e < d;e++ ) { Console.Write(b); } for (int f = a-d; f <= a&&f>=0; f--) { Console.Write(c); } Console.Write("\n"); } Console.ReadLine(); } } }
★
★★★
★★★★★
★★★★★★★
★★★★★★★★★
★★★★★★★
★★★★★
★★★
★
namespace _2017_2_24__for循环的嵌套___打印菱形 { class Program { static void Main(string[] args) { Console.Write("请输入一个数字:"); int a = Convert.ToInt32(Console.ReadLine()); String b="○";String c="★"; for (int d = 1; d <= a;d++ ) { for (int e = a - 1; e >= d ;e-- ) { Console.Write(b); } for (int f = 1; f <=(d * 2-1);f++ ) { Console.Write(c); } Console.Write("\n"); } for (int g = 1; g < a;g++ ) { for (int h = 1; h <= g;h++ ) { Console.Write(b); } for (int i =1; i <=( (a-g) *2 - 1);i++ ) { Console.Write(c); } Console.Write(‘\n‘); } Console.ReadLine(); } } }
2017-2-24 C#基础 for循环的嵌套
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。