Untitled
unknown
python
3 years ago
6.9 kB
6
Indexable
///*
// C# Conditions and If Statements
// C# supports the usual logical conditions from mathematics:
// Less than: a < b
// Less than or equal to: a <= b
// Greater than: a > b
// Greater than or equal to: a >= b
// Equal to a == b
// Not Equal to: a != b
// You can use these conditions to perform different actions for different decisions.
// C# has the following conditional statements:
// Use if to specify a block of code to be executed, if a specified condition is true
// Use else to specify a block of code to be executed, if the same condition is false
// Use else if to specify a new condition to test, if the first condition is false
// Use switch to specify many alternative blocks of code to be executed
//*/
///*
// *
// * if (condition)
//{
// // block of code to be executed if the condition is True
//}
// */
////Console.Write("Podaj 1 liczbe: ");
////var liczba1 = int.Parse(Console.ReadLine());
////Console.Write("Podaj 2 liczbe: ");
////var liczba2 = int.Parse(Console.ReadLine());
////// Sprawdzam liczby1 wieksza niz liczba2
////if (liczba1 > liczba2)
////{
//// // Kawalek programu ktory wykona sie tylko jesli warunek instrukcji warunkowej bedzie prawdziwy
//// Console.WriteLine($"{liczba1} jest wieksza niz {liczba2}");
////}
////else if (liczba2 > liczba1)
////{
//// Console.WriteLine($"{liczba2} jest wieksza niz {liczba1}");
////}
////else
////{
//// Console.WriteLine($"Obie liczby są równe");
////}
//// Wprowadz wartosc czasu i przygotuj powitanie w zaleznosci od wartości
//// dzień dobry - 0 - 20
//// milego dnia 21 - 40
//// milego popołudnia 41 - 60
//// milego wieczoru 61 - 80
//// reszta milej nocy
//// i, lub, zaprzeczenie
//// i (&&) -> 1 i 2 warunek musi byc prawda
//// lub || -> 1 lub 2 warunek musi byc prawda
//// zaprzeczenie !(warunek) -> odwraca dzialanie
//using System.Linq.Expressions;
//Console.WriteLine("Cwiczenie 3");
//Console.Write("Podaj wartosc czasu: ");
//// wczytaj wartosc
//var czas = int.Parse(Console.ReadLine());
//// instrukcja na przywitanie
//if (czas >= 0 && czas <= 20)
// Console.WriteLine("Dzień dobry");
//else if (czas >= 20 && czas <= 40)
// Console.WriteLine("Miłego dnia");
//else if (czas > 40 && czas <= 60)
// Console.WriteLine("Miłego popołudnia");
//else if (czas > 60 && czas <= 80)
// Console.WriteLine("Miłego wieczoru");
//else if (czas > 80 && czas <= 100)
// Console.WriteLine("Miłego nocy");
//else
// Console.WriteLine("Wartosc za duza lub za mala");
///*
// *
// * This is how it works:
//The switch expression is evaluated once
//The value of the expression is compared with the values of each case
//If there is a match, the associated block of code is executed
//The break and default keywords will be described later in this chapter
//switch(expression)
//{
// case x:
// // code block
// break;
// case y:
// // code block
// break;
// default:
// // code block
// break;
//}
// */
//// variable = (condition) ? expressionTrue : expressionFalse;
////
////
//Console.WriteLine("Sprawdzam który to dzień");
//Console.Write("Wprowadz liczbe");
//var zmienna = Console.ReadLine();
//switch (zmienna)
//{
// case "1":
// Console.WriteLine("Poniedzialek");
// break;
// case "2":
// Console.WriteLine("Wtorek");
// break;
// case "3":
// Console.WriteLine("Środa");
// break;
// case "4":
// Console.WriteLine("Czwartek");
// break;
// case "5":
// Console.WriteLine("Piątek");
// break;
// case "6":
// Console.WriteLine("Sobota");
// break;
// case "7":
// Console.WriteLine("Niedziela");
// break;
// default:
// Console.WriteLine("Zla wartosc");
// break;
//}
//// Kalulator z wyborem dzialania
//Console.WriteLine("Podaj nr dzialania do wykonania: {1-4} - dodawanie,odejmowanie,mnozenie,dzielenie");
//int dzialanie = int.Parse(Console.ReadLine());
//Console.Write("Podaj liczbe 1");
//double liczba1 = double.Parse(Console.ReadLine());
//Console.Write("Podaj liczbe 2");
//double liczba2 = double.Parse(Console.ReadLine());
//switch (dzialanie)
//{
// // dodawanie
// case '1':
// Console.WriteLine($"Suma: {liczba1 + liczba2}");
// break;
// // odejmowanie
// case '2':
// Console.WriteLine($"Różnica: {liczba1 - liczba2}");
// break;
// // mnozenie
// case '3':
// Console.WriteLine($"Wynik: {liczba1 * liczba2}");
// break;
// // dzielenie
// case '4':
// Console.WriteLine($"Wynik: {liczba1 / liczba2}");
// break;
// // Weekend tuz tuz
// default:
// Console.WriteLine("Weekend juz wkrotce");
// break;
//}
// PETLE WHILE FOR, FOREACH,
// Wykonuje sie wtedy i do wtedy kiedy wartosc logiczna jest PRAWDA
var licznik = 0;
while (licznik <= 5)
{
Console.WriteLine("Cześć");
await Task.Delay(500);
licznik++; // licznik = licznik + 1
}
// FOR
for (int i = 0; i <= 20; i++)
{
Console.WriteLine($"Lece po raz: {i}");
}
// Ćwiczenie 1
// Wczytaj od użytkownika licznik i wyświetl WESOLYCH SWIAT 'licznik' razy
// PRZEZ PELTE FOR
var ileRazy = Convert.ToInt64(Console.ReadLine());
for(int i = 0; i < ileRazy; i++)
{
Console.WriteLine("!WESOLYCH SWIAT!");
}
// PRZEZ PELTE WHILE
while(ileRazy > 0)
{
Console.WriteLine("!WESOLYCH SWIAT!");
ileRazy--; // ileRazy = ileRazy - 1;
}
// Ćwiczenie 2
// Wypisz sume kolejnych 55 liczb -> może byc na koniec / moze byc w petli
int suma = 0;
for (int i = 0; i <= 55; i++)
suma += i; // suma = suma + i
Console.WriteLine($"Suma: {suma}");
// TABLICE
// Czyli zmienna która przechowuje zbiór takich samych wartości
int[] liczbyCalkowite = new int[] { 0,1,2,3,4,5,6,7,8,9 };
int[] liczby = new int[]{ 0,0,0,0,0,0,2,3,1,3,4,5 };
int[] tablicaNa5Cyft = new int[5] { 0,1,2,3,4 };
// PRZYKLADY Z INNYM TYPEM DANYCH
string[] slowka = new string[] { "czesc", "ciao", "bonjure", "hello", "sup"};
bool[] odpowiedziDoTestu = new bool[] { false, false, false, true, false, true };
// Ćwiczenie 1
// Podaj wielkosc tablicy do utworzenia (ile elementów):
// Podlicz sume wszystkich elementów wprowadzonych przez uzytkownika
Console.WriteLine("Ćwiczenie 1 TABLICE");
Console.Write("Podaj wielkosc tablicy do utworzenia (ile elementów):");
var wielkosc = Convert.ToInt32(Console.ReadLine());
Console.Write("Podaj {0} elementów do tablicy: ", wielkosc);
int[] tablica = new int[wielkosc];
var sumaElementow = 0;
for(int i = 0; i < wielkosc; i++)
{
Console.Write("Element - {0} - ", i);
var liczba = Convert.ToInt32(Console.ReadLine());
tablica[i] = liczba;
sumaElementow += liczba;
}
Console.WriteLine($"Suma elementów to: {sumaElementow}");
Console.ReadKey(); // Zabezpiecza przed auto zamknieciem consoli po programieEditor is loading...