Условный оператор. Циклы. (C-Sharp): различия между версиями
Перейти к навигации
Перейти к поиску
(→Циклы) |
|||
(не показаны 3 промежуточные версии этого же участника) | |||
Строка 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(" | + | 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> | ||
== Циклы == | == Циклы == | ||
+ | * [https://docs.microsoft.com/ru-ru/dotnet/csharp/language-reference/statements/iteration-statements Циклы (C# reference)] | ||
<syntaxhighlight lang="c#" line> | <syntaxhighlight lang="c#" line> | ||
− | + | using System; | |
− | + | ||
− | + | namespace HelloCS | |
{ | { | ||
− | Console.WriteLine(i); | + | class Program |
− | + | { | |
− | } | + | static void Main(string[] args) |
+ | { | ||
+ | Console.WriteLine("while loop"); | ||
+ | int i = 1; | ||
+ | while (i < 11) | ||
+ | { | ||
+ | Console.WriteLine(i); | ||
+ | i++; | ||
+ | } | ||
− | Console.WriteLine("do while loop"); | + | Console.WriteLine("do while loop"); |
− | i = 1; | + | i = 1; |
− | do | + | do |
− | { | + | { |
− | + | Console.WriteLine(i); | |
− | + | i++; | |
− | } while (i < 11); | + | } while (i < 11); |
− | Console.WriteLine("for loop"); | + | Console.WriteLine("for loop"); |
− | for (int j = 1; j < 11; j++) | + | for (int j = 1; j < 11; j++) |
− | { | + | { |
− | + | Console.WriteLine(j); | |
+ | } | ||
+ | } | ||
+ | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Текущая версия на 16:02, 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 using System;
2
3 namespace HelloCS
4 {
5 class Program
6 {
7 static void Main(string[] args)
8 {
9 Console.WriteLine("while loop");
10 int i = 1;
11 while (i < 11)
12 {
13 Console.WriteLine(i);
14 i++;
15 }
16
17 Console.WriteLine("do while loop");
18 i = 1;
19 do
20 {
21 Console.WriteLine(i);
22 i++;
23 } while (i < 11);
24
25 Console.WriteLine("for loop");
26 for (int j = 1; j < 11; j++)
27 {
28 Console.WriteLine(j);
29 }
30 }
31 }
32 }