Go back to the Main

3

This is my main that already includes a flag to leave the program (follow):

static void Main(string[] args) {

  string[] nombre;
  int opcion, tope = 0, cantidad;
  bool seguir = true;



  Console.WriteLine("Ingrese la cantidad: ");

  cantidad = Convert.ToInt32(Console.ReadLine());
  int[] vector = new int[cantidad];
  nombre = new string[cantidad];
  int[, ] matriz = new int[cantidad, 5];


  while (seguir) {

   Console.WriteLine("\t\t1-Reset");
   Console.WriteLine("\t\t2-Salir");

   opcion = Convert.ToInt32(Console.ReadLine());
   switch (opcion) {
    case1:


    string reset = Console.ReadLine();

    if (reset == "SI") {
     //CODIGO

    }
    Console.ReadLine();


    break;

    case2;
    seguir = false;
    break;

What I'm looking for is that when I write yes, I go back to the beginning to ask for quantity and start from 0 (reset the matrix), and if it's not, go back to the normal menu, it needs to be with a while or flag, I can not use a goto

    
asked by eleaefe 30.06.2017 в 17:30
source

3 answers

4

The best thing you can do is add a flag primeravez = true , and move the whole sizing part of the vectors inside the while, like this:

while (seguir) 
{
    if (primeravez == true)
    {
        Console.WriteLine("Ingrese la cantidad: ");
        cantidad = Convert.ToInt32(Console.ReadLine());
        vector = new int[cantidad];
        nombre = new string[cantidad];
        matriz = new int[cantidad, 5];
        primeravez = false;
    } 
    ...
    if (reset == "SI") {
        //CODIGO
        primeravez = true;
        }
    ...
}
    
answered by 30.06.2017 / 19:55
source
2

Just make a nested while

using System; 
public class TuClase
{
    public static void Main()
    {
        string[] nombre;

        int opcion;
        int cantidad;
        int tope;

        int[] vector;
        int[,] matriz;

        int estado = 0;

        do {
            Console.WriteLine("Ingrese la cantidad: ");
            cantidad = Convert.ToInt32(Console.ReadLine());
            nombre = new string[cantidad];
            vector = new int[cantidad];
            matriz = new int[cantidad, 5];

            do {
                Console.WriteLine("\t\t1-Reset");
                Console.WriteLine("\t\t2-Salir");

                opcion = Convert.ToInt32(Console.ReadLine());
                estado = SEGUIR;

                switch (opcion) {
                case REINICIAR:
                    Console.WriteLine("Esta seguro que desea reiniciar las jugadas? [S/N]");
                    Console.WriteLine();
                    if(Console.ReadLine().ToUpper() == "S")
                        estado = REINICIAR;
                    break;
                case SALIR:
                    estado = SALIR;
                    break;
                }
            } while(estado == SEGUIR);

        } while(estado == REINICIAR);
    }

    private const int REINICIAR = 1;
    private const int SALIR = 2;
    private const int SEGUIR = 3;
}
    
answered by 30.06.2017 в 19:27
-1

It is not necessary to use goto , in fact, its use is discouraged by experts in the field.

You could simply restart the Main() function, like this:

if (reset == "SI")
{
    Main(args);
}

What this fragment will do is call the function Main() recursively.

By using recursion, "the function calls itself," which will allow all variables and statements to return to their original state.

    
answered by 30.06.2017 в 17:54