标签:
int a = 1;
for (int i = 0; i < 10; i++) //外部循环
{
a++;
for (int j = 0; j < 3; j++)//内部循环
{
a--;
}
}
注意:一般循环嵌套一层足够,多层会晕,机器会卡
把循环体的概念给想明白
外部循环执行一次,内部循环会执行全部次数(例如:上面的,3 * 10)
练习1
让用户输入一个奇数,打印菱形,最长的行内容个数为用户输入的个数,并且由英文字母拼接而成
比如用户输入了7
A
ABA
ABCBA
ABCDCBA
ABCBA
ABA
A
Console.Write("请输入一个奇数:"); int a = Convert.ToInt32(Console.ReadLine()); if (a % 2 != 0) { for (int i = 1; i <= (a + 1) / 2; i++) { for (int b = 1; b <= ((a + 1) / 2 - i); b++) { Console.Write(" "); } char c = ‘a‘; for (int d = 1; d < i ; d++) { Console.Write(c); c++; } for (int e = 1; e <= 26; e++) { Console.Write(c); if (c == ‘a‘) { break; } c--; } Console.WriteLine(); } for (int i = 1; i < (a + 1) / 2; i++) { for (int b = 1; b <= i; b++) { Console.Write(" "); } char c = ‘a‘; for (int d = 1; d < (a + 1) / 2- i; d++) { Console.Write(c); c++; } for (int e = 1; e <= 26; e++) { Console.Write(c); if (c == ‘a‘) { break; } c--; } Console.WriteLine(); } } else { Console.WriteLine("您输入的不是奇数!"); } //char a = ‘a‘; //for (int i = 1; i <= 26; i++) //{ // Console.Write(a); // a++; //} Console.ReadLine(); } } }
标签:
原文地址:http://www.cnblogs.com/songfengyao/p/5522208.html