.NET 4.x Возврат к определенной точке кода - C#
Формулировка задачи:
Как можно сделать возврат к определенным строчка кода(думаю по коду понятно будет куда) (ЦИКЛЫ НЕ ПРЕДЛАГАТЬ!!!)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
int b;
int[] a = new int[5];
for (int i = 1; i <= 5; i++)
{
a[i] = i;
}
Console.WriteLine("Куда пойти"); // сюда надо вернутся
Console.WriteLine(a[1] +". Домой");
Console.WriteLine(a[2] + ". На работу");
Console.WriteLine(a[3] + ". В магазин");
Console.WriteLine(a[4] + ". В парк");
Console.WriteLine(a[5] + ". На реку");
Console.WriteLine("Введите цифру:");
b = Convert.ToInt32(Console.ReadLine());
if (b == 1)
{
Console.WriteLine("Вы пришли домой");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы вернутся");
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)
{
//отсюда
}
else
{
Console.WriteLine("Вы дома");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы вернутся");//отсюда
}
}
else if(b==2)
{
Console.WriteLine("Вы пришли на работу");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы сбежать с работы");
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)//отсюда
{
}
else
{
Console.WriteLine("Вы работаете");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы сбежать с работы");//отсюда
}
}
else if(b==3)
{
Console.WriteLine("Вы пришли в Магазин");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы уйти, потому что у вас денег все равно нет. А чтобы были надо РАБОТАТЬ!!!");
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)
{
//отсюда
}
else
{
Console.WriteLine("Вы в магазине. Без денег ");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы уйти");//отсюда
}
}
else if(b==4)//отсюда
{
Console.WriteLine("Вы пошли в парк");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы перестать наслаждаться свежим воздухом");
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)
{
}
else
{
Console.WriteLine("Вы гуляете в парке");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы перестать наслаждаться свежим воздухом");
}
}
else if(b==5)//отсюда
{
Console.WriteLine("Вы пришли на реку");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы вернутся");
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)//отсюда
{
}
else
{
Console.WriteLine("Вы на реке");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы вернутся");
}
}
else
{
Console.WriteLine("Неправильный ввод");
}
}
}
}
Вопрос потерял актуальность. Вот к чему я пришел(и да с ЦИКЛАМИ!!)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
int b;
int[] a = new int[6];
for (int i = 1; i < 6; i++)
{
a[i] = i;
}
restart:
Console.WriteLine("Куда пойти");
Console.WriteLine(a[1] +". Домой");
Console.WriteLine(a[2] + ". На работу");
Console.WriteLine(a[3] + ". В магазин");
Console.WriteLine(a[4] + ". В парк");
Console.WriteLine(a[5] + ". На реку");
Console.WriteLine("Введите цифру:");
b = Convert.ToInt32(Console.ReadLine());
if (b == 1)
{
Console.WriteLine("Вы пришли домой");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы вернутся");
while (b != 0)
{
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)
{
goto restart;
}
else
{
Console.WriteLine("Вы дома");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы вернутся");
}
}
}
else if(b==2)
{
Console.WriteLine("Вы пришли на работу");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы сбежать с работы");
while (b != 0)
{
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)
{
goto restart;
}
else
{
Console.WriteLine("Вы работаете");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы сбежать с работы");
}
}
}
else if(b==3)
{
Console.WriteLine("Вы пришли в Магазин");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы уйти, потому что у вас денег все равно нет. А чтобы были надо РАБОТАТЬ!!!");
while (b != 0)
{
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)
{
goto restart;
}
else
{
Console.WriteLine("Вы в магазине. Без денег ");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы уйти");
}
}
}
else if(b==4)
{
Console.WriteLine("Вы пошли в парк");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы перестать наслаждаться свежим воздухом");
while (b != 0)
{
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)
{
goto restart;
}
else
{
Console.WriteLine("Вы гуляете в парке");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы перестать наслаждаться свежим воздухом");
}
}
}
else if(b==5)
{
Console.WriteLine("Вы пришли на реку");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы вернутся");
while (b != 0)
{
b = Convert.ToInt32(Console.ReadLine());
if (b == 0)
{
goto restart;
}
else
{
Console.WriteLine("Вы на реке");
Console.WriteLine();
Console.WriteLine("Нажмите 0 чтобы вернутся");
}
}
}
else
{
Console.WriteLine("Неправильный ввод");
goto restart;
}
}
}
}Решение задачи: «.NET 4.x Возврат к определенной точке кода»
textual
Листинг программы
class Program
{
static void Main(string[] args)
{
WhereToGo();
}
public static void Go(int command)
{
switch (command)
{
case 1:
Console.WriteLine("Вы пришли домой");
break;
case 2:
Console.WriteLine("Вы пришли на работу");
break;
case 3:
Console.WriteLine("Вы пришли в магазин");
break;
case 4:
Console.WriteLine("Вы пришли в парк");
break;
case 5:
Console.WriteLine("Вы пришли на реку");
break;
default:
Console.WriteLine("Неизвестная команда");
break;
}
WhereToGo();
}
public static void WhereToGo()
{
Console.Write(
"\nКуда пойти\n" +
"1. Домой\n" +
"2. На работу\n" +
"3. В магазин\n" +
"4. В парк\n" +
"5. На реку\n\n" +
"Введите число: ");
int command = Convert.ToInt32(Console.ReadLine());
Go(command);
}
}