Условный оператор. Циклы. (C-Sharp): различия между версиями
Перейти к навигации
Перейти к поиску
Строка 4: | Строка 4: | ||
* [https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/keywords/switch switch (справочник по C#)] | * [https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/keywords/switch switch (справочник по C#)] | ||
<syntaxhighlight lang="c#" line> | <syntaxhighlight lang="c#" line> | ||
− | + | using System; | |
− | + | ||
− | + | namespace HelloCS | |
− | |||
{ | { | ||
− | Console.WriteLine("Water is boiling."); | + | class Program |
− | } | + | { |
− | else | + | static void Main(string[] args) |
− | { | + | { |
− | + | Console.WriteLine("Please enter water temperature in degrees celsius::"); | |
− | } | + | string tempString = Console.ReadLine(); |
+ | int temp = Convert.ToInt32(tempString); | ||
+ | if (temp > 65) | ||
+ | { | ||
+ | Console.WriteLine("Water is boiling."); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.WriteLine("Not hot enough."); | ||
+ | } | ||
+ | |||
+ | Console.WriteLine((temp % 2 == 0) ? "Temperature is even" : "Temperature is odd"); | ||
− | switch (DateTime.Now.DayOfWeek) | + | switch (DateTime.Now.DayOfWeek) |
− | { | + | { |
− | + | case DayOfWeek.Saturday: | |
− | + | case DayOfWeek.Sunday: | |
− | + | Console.WriteLine("The weekend"); | |
− | + | break; | |
− | + | case DayOfWeek.Monday: | |
− | + | Console.WriteLine("The first day of the work week."); | |
− | + | break; | |
− | + | case DayOfWeek.Friday: | |
− | + | Console.WriteLine("The last day of the work week."); | |
− | + | break; | |
− | + | default: | |
− | + | Console.WriteLine("The middle of the work week."); | |
− | + | break; | |
+ | } | ||
+ | } | ||
+ | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Версия 11:53, 9 июня 2021
Условный оператор
1 using System;
2
3 namespace HelloCS
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 Console.WriteLine("Please enter water temperature in degrees celsius::");
10 string tempString = Console.ReadLine();
11 int temp = Convert.ToInt32(tempString);
12 if (temp > 65)
13 {
14 Console.WriteLine("Water is boiling.");
15 }
16 else
17 {
18 Console.WriteLine("Not hot enough.");
19 }
20
21 Console.WriteLine((temp % 2 == 0) ? "Temperature is even" : "Temperature is odd");
22
23 switch (DateTime.Now.DayOfWeek)
24 {
25 case DayOfWeek.Saturday:
26 case DayOfWeek.Sunday:
27 Console.WriteLine("The weekend");
28 break;
29 case DayOfWeek.Monday:
30 Console.WriteLine("The first day of the work week.");
31 break;
32 case DayOfWeek.Friday:
33 Console.WriteLine("The last day of the work week.");
34 break;
35 default:
36 Console.WriteLine("The middle of the work week.");
37 break;
38 }
39 }
40 }
41 }
Циклы
1 Console.WriteLine("while loop");
2 i = 1;
3 while (i < 11)
4 {
5 Console.WriteLine(i);
6 i++;
7 }
8
9 Console.WriteLine("do while loop");
10 i = 1;
11 do
12 {
13 Console.WriteLine(i);
14 i++;
15 } while (i < 11);
16
17 Console.WriteLine("for loop");
18 for (int j = 1; j < 11; j++)
19 {
20 Console.WriteLine(j);
21 }