Ayuda números aleatorios

Iniciado por Delikatovic, 24 Noviembre 2016, 19:18 PM

0 Miembros y 1 Visitante están viendo este tema.

Delikatovic

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?

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;
        }
    }
}

okik

#11
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 (csharp) [Seleccionar]

public static int Opcion3(ref string x, int y)
       {
         int z; //<--- integer

.....
           return z;
}


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:
 cant = Console.ReadLine();
cant lo has declarado como integer y console.ReadLine() devuelve un valor String, luego hay que convertirlo a Integer.

Código (csharp) [Seleccionar]
 cant = Convert.ToInt32(Console.ReadLine());

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


Código (csharp) [Seleccionar]

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

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

               Console.WriteLine("{1}{0}{2}{0}{3}{0}{4}{0}{0}{5}", Environment.NewLine,
                   "Opcion 1: Incremento de valor",
                   "Opcion 2: Comparación de números",
                   "Opcion 3: Repetir texto X veces",
                   "Opcion 4: ¿",
                    "Elija una opcion:");
               op = Console.ReadKey().KeyChar;

               switch (op)
               {
                   case '1': //Incremento de valor
                       Opcion1();
                       break;
                   case '2'://Comparación de números
                        int Valor1;
                        int Valor2;
                       Console.WriteLine(Environment.NewLine);
                       Console.WriteLine("Introduce el valor 1:");
                       Valor1 = Convert.ToInt32(Console.ReadLine());
                       Console.WriteLine("Introduce el valor 2:");
                       Valor2 = Convert.ToInt32(Console.ReadLine());
                       Opcion2(Valor1, Valor2);
                       break;
                   case '3'://Repetir texto X veces
                       Console.WriteLine(Environment.NewLine);
                       Console.WriteLine("Introduce texto:");
                       tex = Console.ReadLine();
                       Console.WriteLine("Introduce cantidad:");
                       cant = Convert.ToInt32(Console.ReadLine());

                       resultado = Opcion3(tex, cant);
                       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(int a, int b)
       {
       
         // int c = string.Compare(a, b); //para comparar textos
            int c = 0;
         if ((a < b)) c = 1;
         if ((a > b)) c = 0;
           switch (c)
           {
               case 1:
                   Console.WriteLine(Environment.NewLine);
                   Console.WriteLine("{0} es menor que {1}", a, b);
                   break;
               case 0:
                   Console.WriteLine(Environment.NewLine);
                   Console.WriteLine("{0} es mayor que {1}", a, b);
                   break;
           }
       }

       public static int Opcion3(string x, int y)
       {
           int z;

           for (int i = 0; i < y; i++)
           {
               Console.Write("{0}{1}", x , Convert.ToChar(ConsoleKey.Spacebar));
           }

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

           z = x.Length;

           return z;
       }
   }
}



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 (csharp) [Seleccionar]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
   class Program
   {
       static void Main(string[] args)
       {
           Pregunta();
       }

       public static  void Pregunta()
       {


        char op = '0';
           while (op >= 4 | op < 1)
         {
            Console.WriteLine();
            Console.WriteLine("{1}{0}{2}{0}{3}{0}{4}{0}{0}{5}", Environment.NewLine,
           "Opcion 1: Incremento de valor",
           "Opcion 2: Comparación de números",
           "Opcion 3: Repetir texto X veces",
           "Opcion 4: ¿",
           "Elija una opcion:");
               
           op = Console.ReadKey().KeyChar;
 
               switch (op)
               {
                   case '1': //Incremento de valor
                       Opcion1();
                       break;
                   case '2'://Comparación de números
                       int Valor1;
                       int Valor2;
                       Console.WriteLine(Environment.NewLine);
                       Console.WriteLine("Introduce el valor 1:");
                       Valor1 = Convert.ToInt32(Console.ReadLine());
                       Console.WriteLine("Introduce el valor 2:");
                       Valor2 = Convert.ToInt32(Console.ReadLine());
                       Opcion2(Valor1, Valor2);
                       break;
                   case '3'://Repetir texto X veces
                       string tex;
                       int cant;
                       int resultado;
                       Console.WriteLine(Environment.NewLine);
                       Console.WriteLine("Introduce texto:");
                       tex = Console.ReadLine();
                       Console.WriteLine("Introduce cantidad:");
                       cant = Convert.ToInt32(Console.ReadLine());

                       resultado = Opcion3(tex, cant);
                       Console.WriteLine(tex);
                       break;
                   case '4':
                       Opcion4();
                   break;
               }
               Console.WriteLine(Environment.NewLine);
           }
       
           Console.ReadLine();
         
       }

       public static void Opcion1()
       {

           int i = 0;
           Random rdn = new Random();
           int a = rdn.Next(10, 31);
           int b = rdn.Next(10, 31);
           Console.WriteLine(Environment.NewLine);

           for (i = a; i <= b; i++)
           {
               Console.WriteLine(i);
           }
           Console.WriteLine(Environment.NewLine);
           Pregunta();
       }

       public static void Opcion2(int a, int b)
       {

           // int c = string.Compare(a, b); //para comparar textos
           int c = 0;
           if ((a < b)) c = 1;
           if ((a > b)) c = 0;
           switch (c)
           {
               case 1:
                   Console.WriteLine(Environment.NewLine);
                   Console.WriteLine("{0} es menor que {1}", a, b);
                   break;
               case 0:
                   Console.WriteLine(Environment.NewLine);
                   Console.WriteLine("{0} es mayor que {1}", a, b);
                   break;
           }
           Console.WriteLine(Environment.NewLine);
           Pregunta();
       }

       public static int Opcion3(string x, int y)
       {
           int z;

           for (int i = 0; i < y; i++)
           {
               Console.Write("{0}{1}", x, Convert.ToChar(ConsoleKey.Spacebar));
           }

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

           z = x.Length;
           Console.WriteLine(Environment.NewLine);
           Pregunta();
           return z;
         
       }
       public static void Opcion4()
       {
           Console.WriteLine(Environment.NewLine);
                 Console.WriteLine("Opción no definida");
        }
 

   }
}


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
    while (op >= 4 | op <= 0)

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