3.09课·········打印矩形,三角形和菱形
按制表格的思维,i代表行,j代表列,先看行,再看列
矩形:
Console.Write("输入一个数字:"); int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++)//n行n列,为矩形 { Console.Write("☆"); } Console.WriteLine(); } Console.ReadLine();
左下直角三角形:
1.
Console.Write("输入一个数字:"); int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++)//n行i列,为三角形 { Console.Write("★"); } Console.WriteLine(); } Console.ReadLine(); Console.WriteLine("```````````````````````````````");
2.只用一个for:
string x = ""; string y = "☆"; for (int i = 1; i <= n;i++ ) { x += y;//循环一次多打印一个"☆" Console.WriteLine(x); } Console.ReadLine();
打印结果如下(两种方法一样):
左上直角三角形:
for (int i = n; i >= 1; i--)//初始条件等于n,符合条件则打印,然后递减,直到不符合条件,不再打印 { for (int j = 1; j <= i; j++)//循环条件是j<=i,j与i有关,i是几就打印多少次"☆" { Console.Write("☆"); } Console.Write("\n"); }
打印结果:
右下直角三角形:
string a = ""; string b = "※"; for (int i = 0; i < n; i++) { for (int j = 1; j <= n - i - 1; j++) { Console.Write(" "); } a = a + b; Console.Write(a); Console.Write("\n"); } Console.ReadLine();
打印结果:
右上直角三角形:
for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) { Console.Write(" "); } for (int f = n - i + 1; f >= 1; f--) { Console.Write("◎"); } Console.Write("\n"); } Console.ReadLine();
打印结果:
菱形:
//菱形上半部分。 Console.Write("请输入一个数:"); int n = int.Parse(Console.ReadLine()); for (int i = 1; i <= n; i++) { for (int b = 1; b <= n - i; b++) { Console.Write("°");//为了方便观察使用一个特殊字符表示(可以换成两个空格) } for (int j = 1; j <= 2 * i - 1; j++) { Console.Write("☆"); } Console.Write("\n");//一行打印结束后换行 } //菱形下半部分,同样为了方便观察使用了与上半部分不同的字符。 for (int p = 1; p < n; p++) { for (int o = 1; o <= p; o++) { Console.Write("°"); } for (int k = 1; k <= 2 * (n - p) - 1; k++) { Console.Write("★"); } Console.Write("\n"); } Console.ReadLine();
打印结果:
Where is a will ,there is a way!