تمرین 28
for (int i = 1; i <= 4; ++i)
{
for (int j = 1; j <= i; j++)
{
Console.Write(" *");
}
Console.WriteLine();
}
Console.ReadKey();
چاپ ستاره
تمرین 29
Console.Write("Number=");
int n = int.Parse(Console.ReadLine());
int CountS = 20;
for (int i = 1; i <= n * 2; i += 2)
{
for (int c = 1; c <= CountS; c++)
{
Console.Write(" ");
}
for (int j = 1; j <= i; ++j)
{
Console.Write("*");
}
Console.WriteLine();
CountS--;
}
Console.ReadKey();
چاپ ستاره
تمرین 30
int CountS = 10, i, j, c;
for (i = 1; i <= 8; i += 2)
{
for (c = 1; c <= CountS; c++)
{
Console.Write(" ");
}
for (j = 1; j <= i; ++j)
{
Console.Write("*");
}
Console.WriteLine();
CountS--;
}
//---------------------------
CountS = 7;
for (i = 7; i >= 1; i -= 2)
{
for (c = 1; c <= CountS; c++)
{
Console.Write(" ");
}
for (j = 1; j <= i; ++j)
{
Console.Write("*");
}
Console.WriteLine();
CountS++;
}
Console.ReadKey();
چاپ ستاره
تمرین 31
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; ++j)
{
if (i == j || i + j == 11)
{
Console.Write(" X");
}
else
Console.Write(" ");
}
Console.WriteLine();
}
Console.ReadKey();
چاپ x
تمرین 32
for (int i = 5; i >= 1; --i)
{
for (int j = 1; j <= i; ++j)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadKey();
چاپ ستاره
تمرین 33
Console.Write("Number=");
int n = int.Parse(Console.ReadLine());
int CountSpace = n;
for (int i = 1; i <= n; ++i)
{
//-------------------------
for (int c = 1; c <= CountSpace; c++)
{
Console.Write(" ");
}
CountSpace--;
//-------------------------
for (int j = 1; j <= i; ++j)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadKey();
چاپ ستاره
تمرین 34
int[] ints = { 15, 27, 34 };
Console.WriteLine(ints);
for (int i = 0; i < 3; i++)
{ Console.WriteLine(ints[i]); }
Console.ReadKey();
آرایه ها که با Bracket [ ] نشان می دهد
آرایهها در سیشارپ یکی از متداولترین ساختارهای داده هستند که برای ذخیرهسازی مجموعهای از مقادیر همنوع استفاده میشوند
تمرین 35
int[] ints = { 15, 27, 34 };
Console.WriteLine(ints);
for (int i = 0; i < ints.Length; i++)
{ Console.WriteLine(ints[i]); }
Console.ReadKey();
در سیشارپ، ویژگی .Length برای به دست آوردن تعداد عناصر موجود در یک آرایه یا تعداد کاراکترهای موجود در یک رشته استفاده میشود
تمرین 36
int[] myNumber = new int[4];
for (int i = 0; i < 4; ++i)
{
Console.Write("number " + i + " =");
myNumber[i] = Convert.ToInt32(Console.ReadLine());
}
//_____________________________________________
Console.WriteLine("\n______________________________________\n");
for (int j = 0; j < 4; ++j)
{
Console.WriteLine("Number [" + j + "] = " + myNumber[j]);
}
Console.ReadKey();
از کاربر چهار تا عدد بگیرد و سپس چاپ کند
تمرین 37
string[] mylist = new string[4];
for (int i = 0; i <= 3; ++i)
{
Console.Write("Name You =");
mylist[i] = Console.ReadLine();
}
//---------------------------------
string NameForSearch;
int KeySearch = -1;
Console.Write("Name You For Search=");
NameForSearch = Console.ReadLine();
for (int i = 0; i < 4; ++i)
{
if (NameForSearch == mylist[i])
{
KeySearch = i;
}
}
//-----------------------
if (KeySearch == -1)
{
Console.WriteLine("not find");
}
else
{
Console.Write("Find " + mylist[KeySearch]);
}
Console.ReadKey();
از کاربر چهار تا اسم دریافت کند و سپس اسم را جستجو کند
تمرین 38
int[] MyListNumber = new int[4];
for (int i = 0; i <= 3; ++i)
{
Console.Write("Number [" + (i + 1) + "]=");
MyListNumber[i] = int.Parse(Console.ReadLine());
}
//----------------------------------------
int Temp = 0;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (MyListNumber[j] > MyListNumber[j + 1])
{
Temp = MyListNumber[j];
MyListNumber[j] = MyListNumber[j + 1];
MyListNumber[j + 1] = Temp;
}
}
}
//------------------------------------
for (int i = 0; i < 4; ++i)
{
Console.WriteLine(MyListNumber[i]);
}
Console.ReadKey();
از کاربر عدد دریافت کند و اعداد را از کوچک به بزرگ مرتب کند
تمرین 39
try
{
Console.WriteLine("Please Enter Person Number :");
int Number = Convert.ToInt32(Console.ReadLine());
string[] Names = new string[Number];
for (int i = 0; i < Number; i++)
{
Console.WriteLine("Please Enter Name " + (i + 1));
Names[i] = Console.ReadLine();
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("********************************");
Console.ResetColor();
foreach (string name in Names)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Hello " + name);
}
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Please Enter Just Number");
Console.ResetColor();
}
Console.ReadKey();
از کاربر تعداد نام دریافت کرده و اسامی آنها را وارد کند
اگر کاربر در تعداد اسم به اشتباه، به جای عدد حروف تایپ کرد برنامه اخطار دهد