Алгоритмы (C-Sharp): различия между версиями
		
		
		
		
		
		Перейти к навигации
		Перейти к поиску
		
				
		
		
	
| Строка 89: | Строка 89: | ||
| === Защита от неверного ввода === | === Защита от неверного ввода === | ||
| <syntaxhighlight lang="c#" line> | <syntaxhighlight lang="c#" line> | ||
| + | using System; | ||
| + | namespace Hello | ||
| + | { | ||
| + |     class Program | ||
| + |     { | ||
| + |         static void Main(string[] args) | ||
| + |         { | ||
| + |             int x; | ||
| + |             do { | ||
| + |                 Console.WriteLine("Введите x>0:"); | ||
| + |                 string str = Console.ReadLine(); | ||
| + |                 Int32.TryParse(str, out x); | ||
| + |                 if (x <= 0) { | ||
| + |                     Console.WriteLine("Неверный ввод"); | ||
| + |                 } | ||
| + |             } while (x <= 0); | ||
| + |         } | ||
| + |     } | ||
| + | } | ||
| </syntaxhighlight> | </syntaxhighlight> | ||
Версия 20:00, 5 июня 2021
Код программ
Сумма вводимых целых чисел
 1 using System;
 2 
 3 namespace Hello
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Console.Write("Введите число слагаемых:");
10             string str = Console.ReadLine();
11             Int32.TryParse(str, out var n);
12 
13             int sum = 0;
14             for(int i = 1; i <= n; i++) {
15                 Console.Write($"Введите слагаемое №{i}:");
16                 str = Console.ReadLine();
17                 Int32.TryParse(str, out var x);
18                 sum += x;
19             }
20 
21             Console.WriteLine($"Сумма равна {sum}");
22         }
23     }
24 }
Произведение целых чисел
 1 using System;
 2 
 3 namespace Hello
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Console.Write("Введите число множителей:");
10             string str = Console.ReadLine();
11             Int32.TryParse(str, out var n);
12 
13             int p = 1;
14             for(int i = 1; i <= n; i++) {
15                 Console.Write($"Введите множитель №{i}:");
16                 str = Console.ReadLine();
17                 Int32.TryParse(str, out var x);
18                 p *= x;
19             }
20 
21             Console.WriteLine($"Произведение равно {p}");
22         }
23     }
24 }
Сколько нечетных среди n введенных
 1 using System;
 2 
 3 namespace Hello
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Console.Write("Введите число множителей:");
10             string str = Console.ReadLine();
11             Int32.TryParse(str, out var n);
12 
13             int count = 0;
14             for(int i = 1; i <= n; i++) {
15                 Console.Write($"Введите целое число:");
16                 str = Console.ReadLine();
17                 Int32.TryParse(str, out var x);
18                 if (x % 2 != 0) {
19                     count++;
20                 }
21             }
22 
23             Console.WriteLine($"Количество нечетных равно {count}");
24         }
25     }
26 }
Защита от неверного ввода
 1 using System;
 2 
 3 namespace Hello
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             int x;
10             do {
11                 Console.WriteLine("Введите x>0:");
12                 string str = Console.ReadLine();
13                 Int32.TryParse(str, out x);
14                 if (x <= 0) {
15                     Console.WriteLine("Неверный ввод");
16                 }
17             } while (x <= 0);
18         }
19     }
20 }