Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Delikatovic en 24 Noviembre 2016, 19:18 pm



Título: Ayuda números aleatorios
Publicado por: Delikatovic en 24 Noviembre 2016, 19:18 pm
No entiendo en qué fallo, para que al elegir Opcion1 me muestre los números aleatorios... Sera por la llamada a la funcion? hago algo mal en Opcion1???
Código:
    class Program
    {
        static void Main(string[] args)
        {
            string op = null;
            int num;
            Console.WriteLine("Elija una opcion:");
            op = Console.ReadKey().ToString();

            switch (op)
            {
                case "1":
                    Opcion1();
                   
                    break;
                case "2":
                    Opcion2();

                    break;
                case "3":

                    break;
                case "4":
                    break;

            }
            Console.ReadLine();
        }
        static void Opcion1()
        {
           
            Random rdn = new Random();
            int a = rdn.Next(10, 30);
            int b = rdn.Next(10, 30);
            Console.WriteLine("Números aleatorios: {0} y {1}", a, b);
           
        }

        static string Opcion2()
        {

        }
    }
}


Título: Re: Ayuda números aleatorios
Publicado por: Slava_TZD en 24 Noviembre 2016, 19:55 pm
Tu problema es que estás asignando un ConsoleKeyInfo a una string.

Código
  1. static void Main(string[] args)
  2. {
  3. Console.WriteLine("Elija una opcion:");
  4. switch (Console.ReadKey().KeyChar)
  5. {
  6. case '1':
  7. Opcion1();
  8. break;
  9. default:
  10. break;
  11.  
  12. }
  13. Console.ReadLine();
  14. }


Título: Re: Ayuda números aleatorios
Publicado por: okik en 25 Noviembre 2016, 00:17 am
Usa Console.ReadKey().KeyChar


Al pulsar 1 crea los números aleatorios:

Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.    class Program
  10.    {
  11.        static void Main(string[] args)
  12.        {
  13.            char op = '\0';
  14.           // int num = 0;
  15.            Console.WriteLine("Elija una opcion:");
  16.            op = Console.ReadKey().KeyChar;
  17.  
  18.            switch (op)
  19.            {
  20.                case '1':
  21.                    Opcion1();
  22.                    break;
  23.                case '2':
  24.                    Opcion2();
  25.                    break;
  26.                case '3':
  27.                    break;
  28.                case '4':
  29.                    break;
  30.            }
  31.  
  32.            Console.ReadLine();
  33.        }
  34.        public static void Opcion1()
  35.        {
  36.  
  37.  
  38.            Random rdn = new Random();
  39.            int a = rdn.Next(10, 30);
  40.            int b = rdn.Next(10, 30);
  41.            Console.WriteLine(Environment.NewLine);
  42.            Console.WriteLine("Números aleatorios: {0} y {1}", a, b);
  43.  
  44.        }
  45.  
  46.  
  47.        public static void Opcion2()
  48.        {
  49.        }
  50.    }
  51. }




Usa Console.ReadLine() en lugar de Console.ReadKey(), pero tienes que pulsar Enter para iniciar la función

Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.    class Program
  10.    {
  11.        static void Main(string[] args)
  12.        {
  13.            int op = 0;
  14.           // int num = 0;
  15.            Console.WriteLine("Elija una opcion:");
  16.            op = Convert.ToInt32(Console.ReadLine());
  17.  
  18.            switch (op)
  19.            {
  20.                case 1:
  21.                    Opcion1();
  22.                    break;
  23.                case 2:
  24.                    Opcion2();
  25.                    break;
  26.                case 3:
  27.                    break;
  28.                case 4:
  29.                    break;
  30.            }
  31.  
  32.            Console.ReadLine();
  33.        }
  34.        public static void Opcion1()
  35.        {
  36.  
  37.  
  38.            Random rdn = new Random();
  39.            int a = rdn.Next(10, 30);
  40.            int b = rdn.Next(10, 30);
  41.            Console.WriteLine(Environment.NewLine);
  42.            Console.WriteLine("Números aleatorios: {0} y {1}", a, b);
  43.  
  44.        }
  45.  
  46.  
  47.        public static void Opcion2()
  48.        {
  49.        }
  50.    }
  51. }
  52.  


También:
Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.    class Program
  10.    {
  11.        static void Main(string[] args)
  12.        {
  13.            string op = null;
  14.           // int num = 0;
  15.            Console.WriteLine("Elija una opcion:");
  16.            op = Console.ReadLine();
  17.  
  18.            switch (op)
  19.            {
  20.                case "1":
  21.                    Opcion1();
  22.                    break;
  23.                case "2":
  24.                    Opcion2();
  25.                    break;
  26.                case "3":
  27.                    break;
  28.                case "4":
  29.                    break;
  30.            }
  31.  
  32.            Console.ReadLine();
  33.        }
  34.        public static void Opcion1()
  35.        {
  36.  
  37.  
  38.            Random rdn = new Random();
  39.            int a = rdn.Next(10, 30);
  40.            int b = rdn.Next(10, 30);
  41.            Console.WriteLine(Environment.NewLine);
  42.            Console.WriteLine("Números aleatorios: {0} y {1}", a, b);
  43.  
  44.        }
  45.  
  46.  
  47.        public static void Opcion2()
  48.        {
  49.        }
  50.    }
  51. }


Título: Re: Ayuda números aleatorios
Publicado por: Delikatovic en 25 Noviembre 2016, 18:14 pm
muchas gracias!! le he añadido un do...while para que me siga preguntando opciones :)
Mira.. tengo otro problemilla, en la opción 2:
Desde el main pedir dos cadenas de caracteres (string).La función deberá convertir las dos cadenas a minúsculas, comparar-las, y devolver, por el tercer parámetro(no con return), la palabra “MENOR” si la 1ª cadena es inferior a la 2ª (alfabéticamente), un “MAYOR”, si es mayor alfabéticamente, y un “IGUAL” si son la misma.

No tengo ni idea por donde empezar.. algun enlace de referencia?


Título: Re: Ayuda números aleatorios
Publicado por: Slava_TZD en 25 Noviembre 2016, 19:41 pm
Código
  1. bool? isit_greater(string a, string b)
  2. {
  3. if (a.Length == b.Length)
  4. return null;
  5. return a.Length > b.Length ? true : false;
  6. }

No es exactamente lo que pides, pero es tu tarea casi resuelta :D googlea mas la próxima vez.


Título: Re: Ayuda números aleatorios
Publicado por: Delikatovic en 25 Noviembre 2016, 20:44 pm
si supieras cuanto googleo...

Estoy creando el código, ahora lo paso a ver si alguien me ayuda


Título: Re: Ayuda números aleatorios
Publicado por: Delikatovic en 25 Noviembre 2016, 21:34 pm
Código
  1. bool? isit_greater(string a, string b)
  2. {
  3. if (a.Length == b.Length)
  4. return null;
  5. return a.Length > b.Length ? true : false;
  6. }

No es exactamente lo que pides, pero es tu tarea casi resuelta :D googlea mas la próxima vez.

Lo tengo a media.. cuando elijo la opcion2 ni me pregunta por el texto a escribir, que hago??
Codigo del main
Código:
case '2':
                    Console.WriteLine("Introduce texto:");
                    texto1= Console.ReadLine();
                    Console.WriteLine("Introduce texto:");
                    texto2= Console.ReadLine();
                    Opcion2(ref texto1, ref texto2, out compara);
Codigo de la funcion Opcion2
Código:
public static void Opcion2( ref string a, ref string b, out int c)
        {
            Console.WriteLine(a.ToLower());
            Console.WriteLine(b.ToLower());
            c = string.Compare(a, b);
            Console.WriteLine(c);
        }

me echan un cable?


Título: Re: Ayuda números aleatorios
Publicado por: okik en 26 Noviembre 2016, 13:40 pm
En el case 2

Código
  1.  Console.WriteLine(Environment.NewLine);
  2.               Console.WriteLine("Introduce texto 1:");
  3. string texto1 = Console.ReadLine();
  4. Console.WriteLine("Introduce texto 2:");
  5. string texto2 = Console.ReadLine();
  6. Opcion2(texto1, texto2);
  7.                break;
  8.  


la función Option2:
Código
  1. public static void Opcion2(string a, string b)
  2.        {
  3.            int c = 0;
  4.            c = string.Compare(a, b);
  5.            switch (c)
  6.            {
  7.                case -1:
  8.                    Console.WriteLine(Environment.NewLine);
  9.                    Console.WriteLine("{0} y {1} --> {2}", a.ToLower(), b.ToLower(), "Son diferentes");
  10.                    break;
  11.                case  0:
  12.                    Console.WriteLine(Environment.NewLine);
  13.                    Console.WriteLine("{0} y {1} --> {2}", a.ToLower(), b.ToLower(), "Son iguales");
  14.                    break;
  15.            }
  16.  
  17.        }
  18.  


Te comento, no  se para que pones el REF y el OUT C en la declaración de la función si es la misma función la que luego muestra el resultado en la consola.

"Ref" es para desde la función introducir un valor o nuevo valor a una variable declarada en otra parte del código. Como tras usar la variable en la función no la usas más desde el 'Case' no entiendo para qué le pones el Ref. El uso correcto de ref sería asi..:

En case 2
Código
  1.  
  2.   Console.WriteLine(Environment.NewLine);
  3.               string a= null;
  4.               string b= null;
  5.                 int c = 0;
  6.                   Opcion2(ref a, ref b, ref c);
  7.  
  8.            switch (c)
  9.            {
  10.                case -1:
  11.                    Console.WriteLine(Environment.NewLine);
  12.                    Console.WriteLine("{0} y {1} --> {2}", a.ToLower(), b.ToLower(), "Son diferentes");
  13.                    break;
  14.                case  0:
  15.                    Console.WriteLine(Environment.NewLine);
  16.                    Console.WriteLine("{0} y {1} --> {2}", a.ToLower(), b.ToLower(), "Son iguales");
  17.                    break;
  18.            }
  19.                    break;
  20.  
  21.  

La función option 2:
Código
  1. public static void Opcion2(ref string a,ref string b, ref int c)
  2.        {
  3.  
  4.            Console.WriteLine(Environment.NewLine);
  5.            Console.WriteLine("Introduce texto 1:");
  6.            a = Console.ReadLine();
  7.  
  8.            Console.WriteLine("Introduce texto 2:");
  9.            b = Console.ReadLine();
  10.            c = string.Compare(a, b);
  11.        }
  12.  

Aunque realmente tampoco sería del todo correcto pues la función en este caso no necesita que a y b tengan ningún valor para trabajar con esas variables. En su lugar usar out:

Código
  1.  
  2. ...
  3. Opcion2(out a, out b, out c);
  4. ...
  5.  

Código
  1. public static void Opcion2(out string a, out string b, out int c)
  2.        {
  3.    ....
  4.        }
  5.  


En resumen Ref es para valores de ida y vuelta, ida para la función y vuelta lo que se establece en la función  o subproceso para esa variable.

Por ejemplo (lo he rectificado, estaba mal hecho):

Código
  1. namespace ConsoleApplication1
  2. {
  3.    class Program
  4.    {
  5.        static void Main(string[] args)
  6.        {
  7.            int a = 5;
  8.            int b = 8;
  9.            int c = 0;
  10.            Multiplicar(ref a, ref b, out c);
  11.            Console.WriteLine("a + 5 = {0}", a);
  12.            Console.WriteLine("b + 5 = {0}", b);
  13.            Console.WriteLine("c= a * b = {0}", c);
  14.            Console.ReadLine();
  15.        }
  16.           public static void Multiplicar(ref int a, ref int b, out int c)
  17.        {
  18.         a+= 5;
  19.         b+= 5;
  20.         c = a * b;
  21.        }
  22.    }
  23. }
  24.  

En este caso A llega a la función como 5 y B como 8, luego le suma 5 a cada uno y establece el nuevo resultado en A y B, luego los multiplica y asigna el valor a C


Título: Re: Ayuda números aleatorios
Publicado por: Delikatovic en 27 Noviembre 2016, 16:39 pm
La verdad es que los ref y out no los tengo claro..


Título: Re: Ayuda números aleatorios
Publicado por: okik en 27 Noviembre 2016, 16:55 pm
La verdad es que los ref y out no los tengo claro..

Hombre, pues ya lo he explicado.

Es una variable de ida y vuelta. Entra en la función con un valor, y cuando se procesa la función sale con otro valor.

Se declara una variable antes de llamar a la función. Pongamos int A= 16;


Luego llamas a la función FunciónX(ref int valor) del modo FunciónX(ref A). Entonces la función trabajará con dicho valor A=16.

En la funciónX, se establece un nuevo valor para A, pongamos A= A * 2. Ahora después de la declaración de la función tendrá valor 32


Código
  1. funciónX(ref int Valor)
  2. {
  3. Valor= Valor * 2
  4. }
  5.  


Código
  1. //A tiene valor 16
  2. int A= 16;
  3.  
  4. //Entra dentro como A=16  y opera como A= 16* 2= 32 y ahora el nuevo valor es 32
  5. FunciónX(ref A);
  6.  
  7. //a partir de aquí el valor de A es 32
  8.  
  9. //Muestra el valor de A que es 32
  10. console.WriteLine(A);




Título: Re: Ayuda números aleatorios
Publicado por: Delikatovic en 27 Noviembre 2016, 17:19 pm
Mira este es el código en total. En la 3º opcion debo hacer uso del ref para un texto, y volverá repetido X veces. Las funciones parecen estar bien, pero me falla algo, solo funciona la opc1, me lo chequeas?
Código:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            char op = '\0';
            string texto1;
            string texto2;
            string tex;
            int cant;
            string resultado;
            int res;
            do
            {

                Console.WriteLine("Elija una opcion:");
                op = Console.ReadKey().KeyChar;

                switch (op)
                {
                    case '1':
                        Opcion1();
                        break;
                    case '2':
                        Console.WriteLine(Environment.NewLine);
                            Console.WriteLine("Introduce texto 1:");
                            texto1 = Console.ReadLine();
                            Console.WriteLine("Introduce texto 2:");
                            texto2 = Console.ReadLine();
                            Opcion2(texto1, texto2);
                       
                        break;
                    case '3':
                        Console.WriteLine(Environment.NewLine);
                            Console.WriteLine("Introduce texto:");
                            tex = Console.ReadLine();
                            Console.WriteLine("Introduce cantidad:");
                            cant = Console.ReadLine();

                        resultado = Opcion3(ref tex, cant.ToString);
                        Console.WriteLine(tex);   
                        break;
                    case '4':
                       
                        break;
                }
                Console.ReadLine();
            }
            while (op != 4);
           
        }
        public static void Opcion1()
        {


            Random rdn = new Random();
            int a = rdn.Next(10, 31);
            int b = rdn.Next(10, 31);
            Console.WriteLine(Environment.NewLine);
           
            for (int i= a; i<= b; i++)
            {
                Console.WriteLine(i);
            }

        }


        public static void Opcion2(string a, string b)
        {
            int c = 0;
            c = string.Compare(a, b);
            switch (c)
            {
                case -1:
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine("{0} y {1} --> {2}", a.ToLower(), b.ToLower(), "MENOR");
                    break;
                case 0:
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine("{0} y {1} --> {2}", a.ToLower(), b.ToLower(), "MAYOR");
                    break;
            }
        }

        public static int Opcion3(ref string x, int y)
        {
            int z;
           
                for(int i=0; i<y; i++)
                {
                Console.Write(x);
                }

            x = String.Concat(Enumerable.Repeat(x, y));

            z = x.Length;

            return z;
        }
    }
}


Título: Re: Ayuda números aleatorios
Publicado por: okik en 27 Noviembre 2016, 18:22 pm
fíjate que  en resultado lo declaras como string:   string resultado;
 
pero en Opcion3 devuelves z como integer.  Entonces te da error por eso.

Código
  1. public static int Opcion3(ref string x, int y)
  2.        {
  3.          int z; //<--- integer
  4.  
  5. .....
  6.            return z;
  7. }
  8.  

En Opcion2 pretendes hacer comparación de cadenas string, si son iguales o no, cuando lo que en realidad quieres hacer comparación de mayor o menor de dos números.


También hay una línea que pones:
Código:
  cant = Console.ReadLine();
cant lo has declarado como integer y console.ReadLine() devuelve un valor String, luego hay que convertirlo a Integer.

Código
  1.  cant = Convert.ToInt32(Console.ReadLine());

Tambiés has declarado  int res = 0;, que de momento no usas para nada


Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.    class Program
  11.    {
  12.        static void Main(string[] args)
  13.        {
  14.            char op = '\0';
  15.            string tex;
  16.            int cant;
  17.            int resultado;
  18.            // int res = 0;
  19.            do
  20.            {
  21.  
  22.                Console.WriteLine("{1}{0}{2}{0}{3}{0}{4}{0}{0}{5}", Environment.NewLine,
  23.                    "Opcion 1: Incremento de valor",
  24.                    "Opcion 2: Comparación de números",
  25.                    "Opcion 3: Repetir texto X veces",
  26.                    "Opcion 4: ¿",
  27.                     "Elija una opcion:");
  28.                op = Console.ReadKey().KeyChar;
  29.  
  30.                switch (op)
  31.                {
  32.                    case '1': //Incremento de valor
  33.                        Opcion1();
  34.                        break;
  35.                    case '2'://Comparación de números
  36.                         int Valor1;
  37.                         int Valor2;
  38.                        Console.WriteLine(Environment.NewLine);
  39.                        Console.WriteLine("Introduce el valor 1:");
  40.                        Valor1 = Convert.ToInt32(Console.ReadLine());
  41.                        Console.WriteLine("Introduce el valor 2:");
  42.                        Valor2 = Convert.ToInt32(Console.ReadLine());
  43.                        Opcion2(Valor1, Valor2);
  44.                        break;
  45.                    case '3'://Repetir texto X veces
  46.                        Console.WriteLine(Environment.NewLine);
  47.                        Console.WriteLine("Introduce texto:");
  48.                        tex = Console.ReadLine();
  49.                        Console.WriteLine("Introduce cantidad:");
  50.                        cant = Convert.ToInt32(Console.ReadLine());
  51.  
  52.                        resultado = Opcion3(tex, cant);
  53.                        Console.WriteLine(tex);
  54.                        break;
  55.                    case '4':
  56.  
  57.                        break;
  58.                }
  59.                Console.ReadLine();
  60.            }
  61.            while (op != 4);
  62.  
  63.        }
  64.        public static void Opcion1()
  65.        {
  66.  
  67.  
  68.            Random rdn = new Random();
  69.            int a = rdn.Next(10, 31);
  70.            int b = rdn.Next(10, 31);
  71.            Console.WriteLine(Environment.NewLine);
  72.  
  73.            for (int i = a; i <= b; i++)
  74.            {
  75.                Console.WriteLine(i);
  76.            }
  77.  
  78.        }
  79.  
  80.  
  81.        public static void Opcion2(int a, int b)
  82.        {
  83.  
  84.          // int c = string.Compare(a, b); //para comparar textos
  85.             int c = 0;
  86.          if ((a < b)) c = 1;
  87.          if ((a > b)) c = 0;
  88.            switch (c)
  89.            {
  90.                case 1:
  91.                    Console.WriteLine(Environment.NewLine);
  92.                    Console.WriteLine("{0} es menor que {1}", a, b);
  93.                    break;
  94.                case 0:
  95.                    Console.WriteLine(Environment.NewLine);
  96.                    Console.WriteLine("{0} es mayor que {1}", a, b);
  97.                    break;
  98.            }
  99.        }
  100.  
  101.        public static int Opcion3(string x, int y)
  102.        {
  103.            int z;
  104.  
  105.            for (int i = 0; i < y; i++)
  106.            {
  107.                Console.Write("{0}{1}", x , Convert.ToChar(ConsoleKey.Spacebar));
  108.            }
  109.  
  110.            x = String.Concat(Enumerable.Repeat(x, y));
  111.  
  112.            z = x.Length;
  113.  
  114.            return z;
  115.        }
  116.    }
  117. }
  118.  
  119.  

Hay cosas que no van bien, en la Opcion1 a veces no funciona




Me he permitido hacer algunos arreglos. Espero que te sirva.

Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.    class Program
  11.    {
  12.        static void Main(string[] args)
  13.        {
  14.            Pregunta();
  15.        }
  16.  
  17.        public static  void Pregunta()
  18.        {
  19.  
  20.  
  21.         char op = '0';
  22.            while (op >= 4 | op < 1)
  23.          {
  24.             Console.WriteLine();
  25.             Console.WriteLine("{1}{0}{2}{0}{3}{0}{4}{0}{0}{5}", Environment.NewLine,
  26.            "Opcion 1: Incremento de valor",
  27.            "Opcion 2: Comparación de números",
  28.            "Opcion 3: Repetir texto X veces",
  29.            "Opcion 4: ¿",
  30.            "Elija una opcion:");
  31.  
  32.            op = Console.ReadKey().KeyChar;
  33.  
  34.                switch (op)
  35.                {
  36.                    case '1': //Incremento de valor
  37.                        Opcion1();
  38.                        break;
  39.                    case '2'://Comparación de números
  40.                        int Valor1;
  41.                        int Valor2;
  42.                        Console.WriteLine(Environment.NewLine);
  43.                        Console.WriteLine("Introduce el valor 1:");
  44.                        Valor1 = Convert.ToInt32(Console.ReadLine());
  45.                        Console.WriteLine("Introduce el valor 2:");
  46.                        Valor2 = Convert.ToInt32(Console.ReadLine());
  47.                        Opcion2(Valor1, Valor2);
  48.                        break;
  49.                    case '3'://Repetir texto X veces
  50.                        string tex;
  51.                        int cant;
  52.                        int resultado;
  53.                        Console.WriteLine(Environment.NewLine);
  54.                        Console.WriteLine("Introduce texto:");
  55.                        tex = Console.ReadLine();
  56.                        Console.WriteLine("Introduce cantidad:");
  57.                        cant = Convert.ToInt32(Console.ReadLine());
  58.  
  59.                        resultado = Opcion3(tex, cant);
  60.                        Console.WriteLine(tex);
  61.                        break;
  62.                    case '4':
  63.                        Opcion4();
  64.                    break;
  65.                }
  66.                Console.WriteLine(Environment.NewLine);
  67.            }
  68.  
  69.            Console.ReadLine();
  70.  
  71.        }
  72.  
  73.        public static void Opcion1()
  74.        {
  75.  
  76.            int i = 0;
  77.            Random rdn = new Random();
  78.            int a = rdn.Next(10, 31);
  79.            int b = rdn.Next(10, 31);
  80.            Console.WriteLine(Environment.NewLine);
  81.  
  82.            for (i = a; i <= b; i++)
  83.            {
  84.                Console.WriteLine(i);
  85.            }
  86.            Console.WriteLine(Environment.NewLine);
  87.            Pregunta();
  88.        }
  89.  
  90.        public static void Opcion2(int a, int b)
  91.        {
  92.  
  93.            // int c = string.Compare(a, b); //para comparar textos
  94.            int c = 0;
  95.            if ((a < b)) c = 1;
  96.            if ((a > b)) c = 0;
  97.            switch (c)
  98.            {
  99.                case 1:
  100.                    Console.WriteLine(Environment.NewLine);
  101.                    Console.WriteLine("{0} es menor que {1}", a, b);
  102.                    break;
  103.                case 0:
  104.                    Console.WriteLine(Environment.NewLine);
  105.                    Console.WriteLine("{0} es mayor que {1}", a, b);
  106.                    break;
  107.            }
  108.            Console.WriteLine(Environment.NewLine);
  109.            Pregunta();
  110.        }
  111.  
  112.        public static int Opcion3(string x, int y)
  113.        {
  114.            int z;
  115.  
  116.            for (int i = 0; i < y; i++)
  117.            {
  118.                Console.Write("{0}{1}", x, Convert.ToChar(ConsoleKey.Spacebar));
  119.            }
  120.  
  121.            x = String.Concat(Enumerable.Repeat(x, y));
  122.  
  123.            z = x.Length;
  124.            Console.WriteLine(Environment.NewLine);
  125.            Pregunta();
  126.            return z;
  127.  
  128.        }
  129.        public static void Opcion4()
  130.        {
  131.            Console.WriteLine(Environment.NewLine);
  132.                  Console.WriteLine("Opción no definida");
  133.         }
  134.  
  135.  
  136.    }
  137. }

Ahora, si pones algo distinto de 1, 2, 3, 4, vuelve a mostrar las opciones y pide que elijas una opción. Una vez elegida la opción y realizada la tarea, de nuevo vuelve a mostrar las opciones y de nuevo pregunta por otra opción.




CORRECCIÓN

Corregida esta línea
Código:
    while (op >= 4 | op <= 0) 

era así:
Código:
    while (op > 4 | op < 1)