Show by console an Array

0
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;

  namespace Ejercicio3
     {
     class Program
     {
       enum direcion { arriba = 1, abajo = 3, derecha = 2, izquieda = 4, 
       abajoDerecha=23, abajoIzquierda=34,arribaDerecha=12,
       arribaIzquieda=14, prohibido };
       static void Main(string[] args)
        {
          string num = "";
        Console.WriteLine("Escribe la direcion que desees tomar");
        num = Console.ReadLine();
        string[] array = num.Split(',');

        direcion d = (direcion)Int32.Parse(num);
        for (int i = 0; i < array.Length; i++)
        {
            Console.WriteLine("{0}", (direcion)int.Parse(num.Substring(i, 1)));
        }

    }
}
}

I want to separate what I type by console by ',' I searched and it is done with the method Split but that way I change it to array .

What I do not know is how to show this array now.

I mean I want you to show me the enums if I enter 1,2,3,4 etc . I understood how to do it shown by a string but I do not know with a array .

I BELIEVE that this fails me;

Console.WriteLine("{0}", (direcion)int.Parse(num.Substring(i, 1)));

This line as I come from java I do not understand it very well. And already if it is to raise the level and do it with arrays even less ...

If you can explain how to do it besides clarifying this line that I do not understand I would appreciate it.

    
asked by winnie 16.10.2018 в 12:10
source

1 answer

0

Good practices of C # recommend using foreach instead of for, in case variables are not modified and you need to iterate through the array (( link ):

  

Best Practice # 4: Use the for loop if you need to iterate over a portion of an array or you need to change the elements of the array in some fashion as you iterate. Use foreach when you do not want to change anything in the array and you need to iterate through all elements.).

In the following url he left you what your code would look like: link

Code:

    using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
       enum direcion { arriba = 1, abajo = 3, derecha = 2, izquieda = 4, 
       abajoDerecha=23, abajoIzquierda=34,arribaDerecha=12,
       arribaIzquieda=14, prohibido };
        public static void Main(string[] args)
        {
            string num = "";
            Console.WriteLine("Escribe la direcion que desees tomar");
            num = "1,2,3,4";
            string[] array = num.Split(',');

            Array.ForEach(array, (item) => {
                Console.WriteLine("{0}", (direcion)int.Parse(item));
            });
        }
    }
}

The first thing you were doing wrong was to work on the for with num, it should be with array [i], then the variable "d" is declared but not used.

    
answered by 16.10.2018 / 12:45
source