
用几个练习题演示一下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 = ; i <= a;i++ )
{
for (int b = ; 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 = ; 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 = ; d <= count; d++)
{ for (int e = count - ; e >= d; e--)
{ Console.Write(b);
} Console.Write(c);
for (int x = ; x < d-; 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 = ; d <= a; d++)
{ for (int e = ; e < d;e++ )
{
Console.Write(b); } for (int f = a-d; f <= a&&f>=; 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 = ; d <= a;d++ )
{
for (int e = a - ; e >= d ;e-- )
{
Console.Write(b);
}
for (int f = ; f <=(d * -);f++ )
{
Console.Write(c);
}
Console.Write("\n");
}
for (int g = ; g < a;g++ )
{
for (int h = ; h <= g;h++ )
{
Console.Write(b);
}
for (int i =; i <=( (a-g) * - );i++ )
{
Console.Write(c);
}
Console.Write('\n');
}
Console.ReadLine();
}
}
}
用for循环嵌套编九九乘法表;
namespace _2017_2_24__for循环___九九乘法表
{
class Program
{
static void Main(string[] args)
{
for (int a = ; a <= ;a++ )
{
for (int b = ; b <= a;b++ )
{
Console.Write(a+"*"+b+"="+(a*b)+"\t");
}
Console.Write("\n");
}
Console.ReadLine();
}
}
}