Использование локальной переменной, которой не присвоено значение - C# (177689)

Узнай цену своей работы

Формулировка задачи:

Использование локальной переменной "b", которой не присвоено значение. Не понимаю как сделать её глобальной
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApp1
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. string n, max, min, x, c,m ;
  12. int n1, max1, min1, x1, c1,m1,b;
  13. Console.WriteLine("Vvedite dlinnu masiva");
  14. n = Console.ReadLine();
  15. Console.WriteLine("Vvedite ogranichenie sverhu");
  16. max = Console.ReadLine();
  17. Console.WriteLine("Vvedite ogranichenie snizu");
  18. min = Console.ReadLine();
  19. n1 = Convert.ToInt32(n);
  20. max1 = Convert.ToInt32(max);
  21. min1 = Convert.ToInt32(min);
  22. int[] arr = new int[n1];
  23. Random rand = new Random();
  24. for (int i = 0; i < arr.Length; i++)
  25. {
  26. arr[i] = rand.Next(min1, max1);
  27. }
  28. for (int g = 0; g < arr.Length; g++)
  29. {
  30. if (arr[g] == 0)
  31. {
  32. x = Console.ReadLine();
  33. x1 = Convert.ToInt32(x);
  34. arr[g] = x1;
  35. }
  36. }
  37. Console.WriteLine("Vvedite nachalniy element podmasiva");
  38. c = Console.ReadLine();
  39. c1 = Convert.ToInt32(c);
  40. Console.WriteLine("Vvedite konechniy element podmasiva");
  41. m = Console.ReadLine();
  42. m1 = Convert.ToInt32(m);
  43. int[] arr1 = new int[m1-c1+1];
  44. Array.ConstrainedCopy(arr, c1, arr1, 0, m1);
  45. Console.WriteLine(arr1.Length);
  46. int max2;
  47. for (int i = 0; i < arr1.Length; ++i)
  48. {
  49. if (arr1[i] > arr1[i++])
  50. {
  51. max2 = arr1[i];
  52. }
  53. else
  54. {
  55. max2 = arr1[i++];
  56. }
  57. b = max2;
  58. }
  59. Console.WriteLine(b);
  60. Console.Read();
  61. }
  62. }
  63. }

Решение задачи: «Использование локальной переменной, которой не присвоено значение»

textual
Листинг программы
  1. for (int i = 0; i < arr1.Length; ++i)
  2.         {
  3.          if(i+1<arr1.Length&&i<arr1.Length-1){
  4.                 if (i+1<arr1.Length&&arr1[i] > arr1[i++])
  5.                 {
  6.                     if (i<arr1.Length)
  7.                         max2 = arr1[i];
  8.                     if(i==arr1Length-1)
  9.                         Console.WriteLine(b);
  10.                    if (arr1[i] == arr1[i++])
  11.                         b = max2;
  12.  
  13.                 }
  14.                 else
  15.                 {
  16.                      if (i+1<arr1.Length)
  17.                         max2 = arr1[i++];
  18.                      if(i==arr1Length-1)
  19.                         Console.WriteLine(b);
  20.                      if (arr1[i] == arr1[i++])
  21.                         b = max2;
  22.  
  23.                 }
  24.             }
  25.                
  26.          }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4.333 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы