.NET 4.x Возврат к определенной точке кода - C#

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

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

Как можно сделать возврат к определенным строчка кода(думаю по коду понятно будет куда) (ЦИКЛЫ НЕ ПРЕДЛАГАТЬ!!!)
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication11
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int b;
  13. int[] a = new int[5];
  14. for (int i = 1; i <= 5; i++)
  15. {
  16. a[i] = i;
  17. }
  18. Console.WriteLine("Куда пойти"); // сюда надо вернутся
  19. Console.WriteLine(a[1] +". Домой");
  20. Console.WriteLine(a[2] + ". На работу");
  21. Console.WriteLine(a[3] + ". В магазин");
  22. Console.WriteLine(a[4] + ". В парк");
  23. Console.WriteLine(a[5] + ". На реку");
  24. Console.WriteLine("Введите цифру:");
  25. b = Convert.ToInt32(Console.ReadLine());
  26. if (b == 1)
  27. {
  28. Console.WriteLine("Вы пришли домой");
  29. Console.WriteLine();
  30. Console.WriteLine("Нажмите 0 чтобы вернутся");
  31. b = Convert.ToInt32(Console.ReadLine());
  32. if (b == 0)
  33. {
  34. //отсюда
  35. }
  36. else
  37. {
  38. Console.WriteLine("Вы дома");
  39. Console.WriteLine();
  40. Console.WriteLine("Нажмите 0 чтобы вернутся");//отсюда
  41. }
  42. }
  43. else if(b==2)
  44. {
  45. Console.WriteLine("Вы пришли на работу");
  46. Console.WriteLine();
  47. Console.WriteLine("Нажмите 0 чтобы сбежать с работы");
  48. b = Convert.ToInt32(Console.ReadLine());
  49. if (b == 0)//отсюда
  50. {
  51. }
  52. else
  53. {
  54. Console.WriteLine("Вы работаете");
  55. Console.WriteLine();
  56. Console.WriteLine("Нажмите 0 чтобы сбежать с работы");//отсюда
  57. }
  58. }
  59. else if(b==3)
  60. {
  61. Console.WriteLine("Вы пришли в Магазин");
  62. Console.WriteLine();
  63. Console.WriteLine("Нажмите 0 чтобы уйти, потому что у вас денег все равно нет. А чтобы были надо РАБОТАТЬ!!!");
  64. b = Convert.ToInt32(Console.ReadLine());
  65. if (b == 0)
  66. {
  67. //отсюда
  68. }
  69. else
  70. {
  71. Console.WriteLine("Вы в магазине. Без денег ");
  72. Console.WriteLine();
  73. Console.WriteLine("Нажмите 0 чтобы уйти");//отсюда
  74. }
  75. }
  76. else if(b==4)//отсюда
  77. {
  78. Console.WriteLine("Вы пошли в парк");
  79. Console.WriteLine();
  80. Console.WriteLine("Нажмите 0 чтобы перестать наслаждаться свежим воздухом");
  81. b = Convert.ToInt32(Console.ReadLine());
  82. if (b == 0)
  83. {
  84. }
  85. else
  86. {
  87. Console.WriteLine("Вы гуляете в парке");
  88. Console.WriteLine();
  89. Console.WriteLine("Нажмите 0 чтобы перестать наслаждаться свежим воздухом");
  90. }
  91. }
  92. else if(b==5)//отсюда
  93. {
  94. Console.WriteLine("Вы пришли на реку");
  95. Console.WriteLine();
  96. Console.WriteLine("Нажмите 0 чтобы вернутся");
  97. b = Convert.ToInt32(Console.ReadLine());
  98. if (b == 0)//отсюда
  99. {
  100. }
  101. else
  102. {
  103. Console.WriteLine("Вы на реке");
  104. Console.WriteLine();
  105. Console.WriteLine("Нажмите 0 чтобы вернутся");
  106. }
  107. }
  108. else
  109. {
  110. Console.WriteLine("Неправильный ввод");
  111. }
  112. }
  113. }
  114. }
Вопрос потерял актуальность. Вот к чему я пришел(и да с ЦИКЛАМИ!!)
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication11
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int b;
  13. int[] a = new int[6];
  14. for (int i = 1; i < 6; i++)
  15. {
  16. a[i] = i;
  17. }
  18. restart:
  19. Console.WriteLine("Куда пойти");
  20. Console.WriteLine(a[1] +". Домой");
  21. Console.WriteLine(a[2] + ". На работу");
  22. Console.WriteLine(a[3] + ". В магазин");
  23. Console.WriteLine(a[4] + ". В парк");
  24. Console.WriteLine(a[5] + ". На реку");
  25. Console.WriteLine("Введите цифру:");
  26. b = Convert.ToInt32(Console.ReadLine());
  27. if (b == 1)
  28. {
  29. Console.WriteLine("Вы пришли домой");
  30. Console.WriteLine();
  31. Console.WriteLine("Нажмите 0 чтобы вернутся");
  32. while (b != 0)
  33. {
  34. b = Convert.ToInt32(Console.ReadLine());
  35. if (b == 0)
  36. {
  37. goto restart;
  38. }
  39. else
  40. {
  41. Console.WriteLine("Вы дома");
  42. Console.WriteLine();
  43. Console.WriteLine("Нажмите 0 чтобы вернутся");
  44. }
  45. }
  46. }
  47. else if(b==2)
  48. {
  49. Console.WriteLine("Вы пришли на работу");
  50. Console.WriteLine();
  51. Console.WriteLine("Нажмите 0 чтобы сбежать с работы");
  52. while (b != 0)
  53. {
  54. b = Convert.ToInt32(Console.ReadLine());
  55. if (b == 0)
  56. {
  57. goto restart;
  58. }
  59. else
  60. {
  61. Console.WriteLine("Вы работаете");
  62. Console.WriteLine();
  63. Console.WriteLine("Нажмите 0 чтобы сбежать с работы");
  64. }
  65. }
  66. }
  67. else if(b==3)
  68. {
  69. Console.WriteLine("Вы пришли в Магазин");
  70. Console.WriteLine();
  71. Console.WriteLine("Нажмите 0 чтобы уйти, потому что у вас денег все равно нет. А чтобы были надо РАБОТАТЬ!!!");
  72. while (b != 0)
  73. {
  74. b = Convert.ToInt32(Console.ReadLine());
  75. if (b == 0)
  76. {
  77. goto restart;
  78. }
  79. else
  80. {
  81. Console.WriteLine("Вы в магазине. Без денег ");
  82. Console.WriteLine();
  83. Console.WriteLine("Нажмите 0 чтобы уйти");
  84. }
  85. }
  86. }
  87. else if(b==4)
  88. {
  89. Console.WriteLine("Вы пошли в парк");
  90. Console.WriteLine();
  91. Console.WriteLine("Нажмите 0 чтобы перестать наслаждаться свежим воздухом");
  92. while (b != 0)
  93. {
  94. b = Convert.ToInt32(Console.ReadLine());
  95. if (b == 0)
  96. {
  97. goto restart;
  98. }
  99. else
  100. {
  101. Console.WriteLine("Вы гуляете в парке");
  102. Console.WriteLine();
  103. Console.WriteLine("Нажмите 0 чтобы перестать наслаждаться свежим воздухом");
  104. }
  105. }
  106. }
  107. else if(b==5)
  108. {
  109. Console.WriteLine("Вы пришли на реку");
  110. Console.WriteLine();
  111. Console.WriteLine("Нажмите 0 чтобы вернутся");
  112. while (b != 0)
  113. {
  114. b = Convert.ToInt32(Console.ReadLine());
  115. if (b == 0)
  116. {
  117. goto restart;
  118. }
  119. else
  120. {
  121. Console.WriteLine("Вы на реке");
  122. Console.WriteLine();
  123. Console.WriteLine("Нажмите 0 чтобы вернутся");
  124. }
  125. }
  126. }
  127. else
  128. {
  129. Console.WriteLine("Неправильный ввод");
  130. goto restart;
  131. }
  132.  
  133. }
  134. }
  135. }

Решение задачи: «.NET 4.x Возврат к определенной точке кода»

textual
Листинг программы
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         WhereToGo();
  6.     }
  7.  
  8.     public static void Go(int command)
  9.     {
  10.         switch (command)
  11.         {
  12.             case 1:
  13.                 Console.WriteLine("Вы пришли домой");
  14.                 break;
  15.  
  16.             case 2:
  17.                 Console.WriteLine("Вы пришли на работу");
  18.                 break;
  19.  
  20.             case 3:
  21.                 Console.WriteLine("Вы пришли в магазин");
  22.                 break;
  23.  
  24.             case 4:
  25.                 Console.WriteLine("Вы пришли в парк");
  26.                 break;
  27.  
  28.             case 5:
  29.                 Console.WriteLine("Вы пришли на реку");
  30.                 break;
  31.  
  32.             default:
  33.                 Console.WriteLine("Неизвестная команда");
  34.                 break;
  35.         }
  36.  
  37.         WhereToGo();
  38.     }
  39.  
  40.     public static void WhereToGo()
  41.     {
  42.         Console.Write(
  43.             "\nКуда пойти\n" +
  44.             "1. Домой\n" +
  45.             "2. На работу\n" +
  46.             "3. В магазин\n" +
  47.             "4. В парк\n" +
  48.             "5. На реку\n\n" +
  49.             "Введите число: ");
  50.  
  51.         int command = Convert.ToInt32(Console.ReadLine());
  52.         Go(command);
  53.     }
  54. }

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


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

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

10   голосов , оценка 4.1 из 5

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

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

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