Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace File_Array2
- {
- class Program
- {
- const string ERROR_MSG = "wrong";
- static int InputIntN(string errorMsg) // Метод на ввод.
- {
- Console.Write("Enter array lenght: ");
- string s = Console.ReadLine();
- int N;
- if (!int.TryParse(s, out N) || N < 1 || N > 100)
- {
- Console.WriteLine(errorMsg);
- return InputIntN(ERROR_MSG);
- }
- return N;
- }
- static void InputElement(int N, int[] numbers, string errorMsg, ref int count)
- {
- string[] arr = new string[N];
- for (int i = 0; i < N; i++)
- {
- Console.WriteLine("Enter array elements: ");
- arr[i] = Console.ReadLine();
- while (!int.TryParse(arr[i], out numbers[i]))
- {
- Console.WriteLine(errorMsg);
- Console.WriteLine("Enter array elements: ");
- arr[i] = Console.ReadLine();
- }
- if (numbers[i] % 42 == 0)
- {
- count++;
- }
- if (i % 2 != 0)
- {
- numbers[i] = 0;
- }
- }
- }
- static void Main()
- {
- do
- {
- int N = InputIntN(ERROR_MSG);
- int[] numbers = new int[N];
- int count = 0;
- InputElement(N, numbers, ERROR_MSG, ref count);
- string str = String.Join(" ", numbers);
- string file_name = "output.txt";
- try
- {
- File.Delete(file_name);
- File.AppendAllText(file_name, str);
- File.AppendAllText(file_name, "\n" + count);
- }
- catch (IOException ex)
- {
- Console.WriteLine(ex.Message);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- Console.WriteLine("To exit from program press ESCAPE. To restart this program press any key.....");
- } while (Console.ReadKey(true).Key != ConsoleKey.Escape); // Повтор решения.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement