elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Únete al Grupo Steam elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Ayuda números aleatorios
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: 1 [2] Ir Abajo Respuesta Imprimir
Autor Tema: Ayuda números aleatorios  (Leído 6,305 veces)
Delikatovic

Desconectado Desconectado

Mensajes: 8


Ver Perfil
Re: Ayuda números aleatorios
« Respuesta #10 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;
        }
    }
}


En línea

okik


Desconectado Desconectado

Mensajes: 462


Ver Perfil
Re: Ayuda números aleatorios
« Respuesta #11 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)





« Última modificación: 27 Noviembre 2016, 20:30 pm por okik » En línea

Páginas: 1 [2] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines