Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _2020_lab1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. int option;
  10.  
  11. while (true)
  12. {
  13. try
  14. {
  15. Console.Write("выберите\n" +
  16. "1. Размер массива вводит пользователь, массив генерируется случайным образом\n" +
  17. "2. Размер массива до 10 элементов генерируется случайным образом, элементы массива вводит пользователь.\n" +
  18. ">>> ");
  19.  
  20. option = Convert.ToInt32(Console.ReadLine());
  21.  
  22. if (option == 1 || option == 2)
  23. { break; }
  24. else
  25. {
  26. Console.WriteLine("выберите 1 или 2");
  27. continue;
  28. }
  29. }
  30. catch
  31. {
  32. Console.WriteLine("не некорректное значение");
  33. continue;
  34. }
  35. }
  36.  
  37. Random rnd = new Random();
  38. int max_negative = -101;
  39.  
  40. if (option == 1)
  41. {
  42.  
  43. int mas_lenght;
  44.  
  45. while (true)
  46. {
  47. Console.Write("размер массива >>> ");
  48. try
  49. {
  50. mas_lenght = Convert.ToInt32(Console.ReadLine());
  51. break;
  52. }
  53.  
  54. catch (Exception ex)
  55. {
  56. Console.WriteLine(ex.Message);
  57. continue;
  58. }
  59. }
  60.  
  61. int[] array = new int[mas_lenght];
  62.  
  63. for (int i = 0; i < array.Length; i++)
  64. {
  65. array[i] = rnd.Next(-100, 100);
  66. Console.Write(array[i] + " ");
  67. if (array[i] < 0 & array[i] > max_negative) { max_negative = array[i]; }
  68. }
  69. }
  70. else
  71. {
  72. int s = rnd.Next(1, 10);
  73. int[] array = new int[s];
  74. Console.WriteLine("количество элементов = " + s);
  75. for (int i = 0; i < array.Length; i++)
  76. {
  77. //Console.Write(i+1 + " элемент = ");
  78.  
  79. while (true)
  80. {
  81. try
  82. {
  83. Console.Write(i + 1 + " элемент (от -100 до 100) = ");
  84. array[i] = Convert.ToInt32(Console.ReadLine());
  85. if (array[i] < -100) { Console.WriteLine("выход из диапазона "); continue; }
  86. break;
  87. }
  88.  
  89. catch (Exception ex)
  90. {
  91. Console.WriteLine(ex.Message);
  92. continue;
  93. }
  94. }
  95. //array[i] = Convert.ToInt32(Console.ReadLine());
  96. if (array[i] < 0 & array[i] > max_negative) { max_negative = array[i]; }
  97. }
  98. }
  99.  
  100. if (max_negative == -101) { Console.WriteLine("\nотрицательных элементов нет"); }
  101. else { Console.Write("\nмаксимум из отрицательных элементов -> " + max_negative); }
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement