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

 

 


Tema destacado: Guía actualizada para evitar que un ransomware ataque tu empresa


  Mostrar Mensajes
Páginas: [1]
1  Programación / Ejercicios / Re: Ejercicios en JAVA en: 19 Marzo 2013, 11:42 am
Andaba dandome una vuelta y pues.. estos ejercicios no estan nada mal.. son divertidos me he hecho unos por si alguien en el futuro necesita ver algun ejemplo de resolverlos..
los que mas me gustaron fueron las piramides.. y el rombo lml

Piramide normal:
Código
  1. import java.util.*;
  2.  
  3. public class PiramideA
  4. {
  5. public static void main(String args[])
  6. {
  7. Scanner cin = new Scanner(System.in);
  8. int n;
  9. System.out.println("*=====================*");
  10. System.out.print("  Altura de la piramide: ");
  11. n = cin.nextInt();
  12. System.out.println();
  13. for(int i=0; i<n; i++)
  14. {
  15. for(int j=0; j<=i; j++)
  16. {
  17. System.out.print("*");
  18. }
  19. System.out.println();
  20. }
  21. }
  22. }

Hacer la piramide alrevez tambien es muy facil..
Código
  1. for(int i=0; i<n; i++)
  2. {
  3.        for(int j=n-i-1; j>=0; j--)
  4.        {
  5.        System.out.print("*");
  6.        }
  7.        System.out.println();
  8. }

El rombo relleno:
Código
  1. import java.util.*;
  2.  
  3. public class Rombo
  4. {
  5. public static void main(String args[])
  6. {
  7. Scanner cin = new Scanner(System.in);
  8. boolean b = false;;
  9. int n;
  10.  
  11. do
  12. {
  13. if(b) cout("El dato ingresado debe ser impar\n"); //para que se vea bonito el rombo :P
  14. cout("Ingrese la diagonal horizontal: ");
  15. n = cin.nextInt();
  16. b = (n%2==0);
  17. }while(b);
  18.  
  19. //calculando los espacios:
  20. String space = "";
  21. for(int i = 0; i<n/2+1; i++)
  22. {
  23. space = space + " ";
  24. }
  25.  
  26. /*
  27. Impares = 2*N +1
  28. Ocuparemos dicha formula para generar los *
  29. */
  30.  
  31. cout("\n Rombo:\n\n");
  32. //formando el rombo:
  33. int tope = n/2 + 1;
  34. for(int i = 0; i < n; i++)
  35. {
  36. if(tope>i)
  37. {
  38. cout(space.substring(i));
  39. for(int j = 0; j<2*i+1; j++)
  40. {
  41. cout("*");
  42. }
  43. }else
  44. {
  45. cout(space.substring(n-i-1));
  46. for(int j = 0; j<2*(n-1-i)+1; j++)
  47. {
  48. cout("*");
  49. }
  50. }
  51. cout("\n");
  52. }
  53.  
  54. }
  55.  
  56.  
  57. public static void cout(String s)
  58. {
  59. System.out.print(s);
  60. }
  61. }
  62.  

Y por ultimo el rombo vacio
Código
  1. //formando el rombo:
  2. int tope = n/2 + 1;
  3. for(int i = 0; i < n; i++)
  4. {
  5. if(tope>i)
  6. {
  7. cout(space.substring(i));
  8. for(int j = 0; j<2*i+1; j++)
  9. {
  10. if(j==2*i || j==0)
  11. cout("*");
  12. else
  13. cout(" ");
  14. }
  15. }else
  16. {
  17. cout(space.substring(n-i-1));
  18. for(int j = 0; j<2*(n-1-i)+1; j++)
  19. {
  20. if(j==2*(n-i-1) || j==0)
  21. cout("*");
  22. else
  23. cout(" ");
  24. }
  25. }
  26. cout("\n");
  27. }


Hey gracias Mad.. me he divertido un rato.. siempre es bueno practicar con unos ejercicios de logica..
salu2
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines