SlideShare a Scribd company logo
1 of 16
Платформата Microsoft .NET и
езикът на програмиране C#
Масиви и списъци
Упражнение if
 Декларирайте две числени променливи
 Въведете ги от конзолата
 Изпишете ги в нарастващ ред
Упражнение if
string s1 = Console.ReadLine();
int x1 = int.Parse(s1);
string s2 = Console.ReadLine();
int x2 = int.Parse(s2);
if (x1>x2)
{
int t = x1;
x1 = x2;
x2 = t;
}
Console.WriteLine(“{0} {1}”,x1,x2);
Упражнение while
 Изпишете степените на числото 2 от нулева до десета
 1, 2, 4, 8...
Упражнение while
int i = 0;
int pow = 1;
while (i<=10)
{
Console.WriteLine(pow);
pow *= 2;
++i;
}
Упражнение for
 Изпишете числата от 1 до 100 и отбележете всички нечетни,
 които се делят на 7 без остатък
Упражнение for
for (int i=1; i<=100; ++i)
{
if ( (i%2==1) && (i%7==0) ) Console.Write(“*** ”);
Console.WriteLine(i);
}
Масиви
 Съвкупност от данни, които могат да бъдат достъпвани по индекс
int[] array1 = new int[5];
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
int[] array3 = { 1, 2, 3, 4 };
for (int i=0; i<array2.Length; ++i)
{
Console.WriteLine(array2[i]);
}
Упражнение масиви
 Декларирайте масив с имената на дните от седмицата
 При въведено число от 1 до 7 изпишете съответния ден
Упражнение масиви
string[] week = { “mon”, “tue”, “wed”, “thu”, “fri”, “sat”, “sun” };
string dayStr = Console.ReadLine();
int day = int.Parse(dayStr);
Console.WriteLine(week[day]);
Упражнение масиви 2
 Въведете пет думи от конзолата и ги запазете в масив
 Изведете ги в обратен ред
 Изведете дължината на най-дългата от тях
Упражнение масиви 2
string[] words = new string[5];
for (int i=0; i< words.Length; ++i)
{
words[i] = Console.ReadLine();
}
int longestWord = 0;
for (int i=words.Length-1; i>=0; --i)
{
Console.WriteLine(words[i]);
if (longestWord<words[i].Length) longestWord=words[i].Length;
}
Console.WriteLine(“the logest word length is {0}”, longestWord);
Многомерни масиви и foreach
 Правоъгълни масиви
int[,] array2D = new int[3,4];
 Масиви от масиви (назъбени масиви/jagged arrays)
int[][] scores = new int[3][];
for (int row = 0; row < scores.Length; ++row)
{
scores[row] = new int[4];
}
https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
Списъци
 За разлика от масивите, сами разширяват размерите си при добавяне на нови елементи
List<int> list = new List<int>();
list.Add(2);
list.Add(3);
list.Add(7);
foreach (int prime in list) // Loop through List with foreach
{
Console.WriteLine(prime);
}
for (int i = 0; i < list.Count; i++) // Loop with for
{
Console.WriteLine(list[i]);
}
Аргументи на командния ред
static int Main(string[] args)
 Въведете една дума от командния ред и проверете дали тя съдържа
единствено символите за малки и големи букви

More Related Content

Viewers also liked (16)

.NET/C#_1
.NET/C#_1.NET/C#_1
.NET/C#_1
 
.NET/C#_15
.NET/C#_15.NET/C#_15
.NET/C#_15
 
.NET/C#_13
.NET/C#_13.NET/C#_13
.NET/C#_13
 
.NET/C#_6
.NET/C#_6.NET/C#_6
.NET/C#_6
 
.NET/C#_18
.NET/C#_18.NET/C#_18
.NET/C#_18
 
.NET/C#_19
.NET/C#_19.NET/C#_19
.NET/C#_19
 
.NET/C#_10
.NET/C#_10.NET/C#_10
.NET/C#_10
 
.NET/C#_9
.NET/C#_9.NET/C#_9
.NET/C#_9
 
.NET/C#_17
.NET/C#_17.NET/C#_17
.NET/C#_17
 
.NET/C#_2
.NET/C#_2.NET/C#_2
.NET/C#_2
 
.NET/C#_20
.NET/C#_20.NET/C#_20
.NET/C#_20
 
.NET/C#_16
.NET/C#_16.NET/C#_16
.NET/C#_16
 
.NET/C#_8
.NET/C#_8.NET/C#_8
.NET/C#_8
 
.NET/C#_12
.NET/C#_12.NET/C#_12
.NET/C#_12
 
.NET/C#_5
.NET/C#_5.NET/C#_5
.NET/C#_5
 
.NET/C#_4
.NET/C#_4.NET/C#_4
.NET/C#_4
 

More from RaynaITSTEP

Project management professional
Project management professionalProject management professional
Project management professionalRaynaITSTEP
 
Project management it step
Project management it stepProject management it step
Project management it stepRaynaITSTEP
 
РАБОТА С ОБЕКТА „ЗАЯВКА“ (2)
РАБОТА С ОБЕКТА „ЗАЯВКА“ (2)РАБОТА С ОБЕКТА „ЗАЯВКА“ (2)
РАБОТА С ОБЕКТА „ЗАЯВКА“ (2)RaynaITSTEP
 
monitoring and diagnostics
monitoring and diagnosticsmonitoring and diagnostics
monitoring and diagnosticsRaynaITSTEP
 
network security
network securitynetwork security
network securityRaynaITSTEP
 
configuring disk sand drivers
configuring disk sand driversconfiguring disk sand drivers
configuring disk sand driversRaynaITSTEP
 
install update and migration to windows 10
install update and migration to windows 10install update and migration to windows 10
install update and migration to windows 10RaynaITSTEP
 
Фотошоп за деца_4
Фотошоп за деца_4Фотошоп за деца_4
Фотошоп за деца_4RaynaITSTEP
 
Структурни кабелни системи
Структурни кабелни системиСтруктурни кабелни системи
Структурни кабелни системиRaynaITSTEP
 
Android introduction
Android introductionAndroid introduction
Android introductionRaynaITSTEP
 
Adobe Illustrator - Урок 2
Adobe Illustrator - Урок 2Adobe Illustrator - Урок 2
Adobe Illustrator - Урок 2RaynaITSTEP
 
Adobe Illustrator - Обобщение
Adobe Illustrator - ОбобщениеAdobe Illustrator - Обобщение
Adobe Illustrator - ОбобщениеRaynaITSTEP
 

More from RaynaITSTEP (20)

Project management professional
Project management professionalProject management professional
Project management professional
 
Project management it step
Project management it stepProject management it step
Project management it step
 
Lean startup
Lean startupLean startup
Lean startup
 
РАБОТА С ОБЕКТА „ЗАЯВКА“ (2)
РАБОТА С ОБЕКТА „ЗАЯВКА“ (2)РАБОТА С ОБЕКТА „ЗАЯВКА“ (2)
РАБОТА С ОБЕКТА „ЗАЯВКА“ (2)
 
DBT_3
DBT_3DBT_3
DBT_3
 
DBT_2
DBT_2DBT_2
DBT_2
 
DBT_1
DBT_1DBT_1
DBT_1
 
monitoring and diagnostics
monitoring and diagnosticsmonitoring and diagnostics
monitoring and diagnostics
 
network security
network securitynetwork security
network security
 
networking
networkingnetworking
networking
 
data security
data securitydata security
data security
 
configuring disk sand drivers
configuring disk sand driversconfiguring disk sand drivers
configuring disk sand drivers
 
boot process
boot process  boot process
boot process
 
install update and migration to windows 10
install update and migration to windows 10install update and migration to windows 10
install update and migration to windows 10
 
virtualization
virtualizationvirtualization
virtualization
 
Фотошоп за деца_4
Фотошоп за деца_4Фотошоп за деца_4
Фотошоп за деца_4
 
Структурни кабелни системи
Структурни кабелни системиСтруктурни кабелни системи
Структурни кабелни системи
 
Android introduction
Android introductionAndroid introduction
Android introduction
 
Adobe Illustrator - Урок 2
Adobe Illustrator - Урок 2Adobe Illustrator - Урок 2
Adobe Illustrator - Урок 2
 
Adobe Illustrator - Обобщение
Adobe Illustrator - ОбобщениеAdobe Illustrator - Обобщение
Adobe Illustrator - Обобщение
 

.NET/C#_3

  • 1. Платформата Microsoft .NET и езикът на програмиране C#
  • 3. Упражнение if  Декларирайте две числени променливи  Въведете ги от конзолата  Изпишете ги в нарастващ ред
  • 4. Упражнение if string s1 = Console.ReadLine(); int x1 = int.Parse(s1); string s2 = Console.ReadLine(); int x2 = int.Parse(s2); if (x1>x2) { int t = x1; x1 = x2; x2 = t; } Console.WriteLine(“{0} {1}”,x1,x2);
  • 5. Упражнение while  Изпишете степените на числото 2 от нулева до десета  1, 2, 4, 8...
  • 6. Упражнение while int i = 0; int pow = 1; while (i<=10) { Console.WriteLine(pow); pow *= 2; ++i; }
  • 7. Упражнение for  Изпишете числата от 1 до 100 и отбележете всички нечетни,  които се делят на 7 без остатък
  • 8. Упражнение for for (int i=1; i<=100; ++i) { if ( (i%2==1) && (i%7==0) ) Console.Write(“*** ”); Console.WriteLine(i); }
  • 9. Масиви  Съвкупност от данни, които могат да бъдат достъпвани по индекс int[] array1 = new int[5]; int[] array2 = new int[] { 1, 3, 5, 7, 9 }; int[] array3 = { 1, 2, 3, 4 }; for (int i=0; i<array2.Length; ++i) { Console.WriteLine(array2[i]); }
  • 10. Упражнение масиви  Декларирайте масив с имената на дните от седмицата  При въведено число от 1 до 7 изпишете съответния ден
  • 11. Упражнение масиви string[] week = { “mon”, “tue”, “wed”, “thu”, “fri”, “sat”, “sun” }; string dayStr = Console.ReadLine(); int day = int.Parse(dayStr); Console.WriteLine(week[day]);
  • 12. Упражнение масиви 2  Въведете пет думи от конзолата и ги запазете в масив  Изведете ги в обратен ред  Изведете дължината на най-дългата от тях
  • 13. Упражнение масиви 2 string[] words = new string[5]; for (int i=0; i< words.Length; ++i) { words[i] = Console.ReadLine(); } int longestWord = 0; for (int i=words.Length-1; i>=0; --i) { Console.WriteLine(words[i]); if (longestWord<words[i].Length) longestWord=words[i].Length; } Console.WriteLine(“the logest word length is {0}”, longestWord);
  • 14. Многомерни масиви и foreach  Правоъгълни масиви int[,] array2D = new int[3,4];  Масиви от масиви (назъбени масиви/jagged arrays) int[][] scores = new int[3][]; for (int row = 0; row < scores.Length; ++row) { scores[row] = new int[4]; } https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
  • 15. Списъци  За разлика от масивите, сами разширяват размерите си при добавяне на нови елементи List<int> list = new List<int>(); list.Add(2); list.Add(3); list.Add(7); foreach (int prime in list) // Loop through List with foreach { Console.WriteLine(prime); } for (int i = 0; i < list.Count; i++) // Loop with for { Console.WriteLine(list[i]); }
  • 16. Аргументи на командния ред static int Main(string[] args)  Въведете една дума от командния ред и проверете дали тя съдържа единствено символите за малки и големи букви