Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace File_Array2
  5. {
  6.     class Program
  7.     {
  8.         const string ERROR_MSG = "wrong";
  9.  
  10.         static int InputIntN(string errorMsg) // Метод на ввод.
  11.         {
  12.             Console.Write("Enter array lenght: ");
  13.             string s = Console.ReadLine();
  14.             int N;
  15.             if (!int.TryParse(s, out N) || N < 1 || N > 100)
  16.             {
  17.                 Console.WriteLine(errorMsg);
  18.                 return InputIntN(ERROR_MSG);
  19.             }
  20.  
  21.             return N;
  22.         }
  23.  
  24.         static void InputElement(int N, int[] numbers, string errorMsg, ref int count)
  25.         {
  26.             string[] arr = new string[N];
  27.  
  28.             for (int i = 0; i < N; i++)
  29.             {
  30.                 Console.WriteLine("Enter array elements: ");
  31.                 arr[i] = Console.ReadLine();
  32.  
  33.                 while (!int.TryParse(arr[i], out numbers[i]))
  34.                 {
  35.                     Console.WriteLine(errorMsg);
  36.                     Console.WriteLine("Enter array elements: ");
  37.                     arr[i] = Console.ReadLine();
  38.                 }
  39.  
  40.                 if (numbers[i] % 42 == 0)
  41.                 {
  42.                     count++;
  43.                 }
  44.  
  45.                 if (i % 2 != 0)
  46.                 {
  47.                     numbers[i] = 0;
  48.                 }
  49.  
  50.  
  51.             }
  52.  
  53.  
  54.         }
  55.  
  56.  
  57.  
  58.         static void Main()
  59.         {
  60.             do
  61.             {
  62.                 int N = InputIntN(ERROR_MSG);
  63.                 int[] numbers = new int[N];
  64.                 int count = 0;
  65.  
  66.                 InputElement(N, numbers, ERROR_MSG, ref count);
  67.  
  68.                 string str = String.Join(" ", numbers);
  69.  
  70.                 string file_name = "output.txt";
  71.  
  72.                 try
  73.                 {
  74.                     File.Delete(file_name);
  75.                     File.AppendAllText(file_name, str);
  76.                     File.AppendAllText(file_name, "\n" + count);
  77.  
  78.                 }
  79.                 catch (IOException ex)
  80.                 {
  81.                     Console.WriteLine(ex.Message);
  82.                 }
  83.                 catch (Exception e)
  84.                 {
  85.                     Console.WriteLine(e.Message);
  86.                 }
  87.  
  88.                 Console.WriteLine("To exit from program press ESCAPE. To restart this program press any key.....");
  89.             } while (Console.ReadKey(true).Key != ConsoleKey.Escape); //  Повтор решения.
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement