fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. /* Christopher Isherwood
  9.   CSC 115
  10.   9/30/2014
  11.   Hour Glass Program
  12.   Homework #1
  13.   This program will print a image of a hour glass depending on the size of the constant
  14.  
  15.   Used ideone.com to check code due to an "jGRASP wedge2 error: command "javac" not found." with jGRASP
  16.   Had to use "public class Main" for the website. Would normally use "public class HourGlass"
  17. */
  18.  
  19. public class Main
  20. {
  21. public static final int SIZE = 17;
  22. public static void main(String[] args) {
  23. end();
  24. top();
  25. vertical();
  26. bottom();
  27. end();
  28. }
  29.  
  30. // Produces ends of the Hour Glass
  31. public static void end()
  32. {
  33. System.out.print("|");
  34. int i;
  35. for (i=1; i <=SIZE*2+2; i++)
  36. {
  37. System.out.print("\"");
  38. }
  39. System.out.println("|");
  40. }
  41.  
  42. // This produces the top half of the hourglass figure
  43. public static void top()
  44. {
  45. for (int j=1; j <=SIZE; j++)
  46. {
  47. int i;
  48. for (i=1; i <=(j*SIZE)/SIZE; i++){
  49. System.out.print(" ");
  50. }
  51. System.out.print("\\");
  52. int k;
  53. for (k=j; k<=SIZE; k++){
  54. System.out.print("::");
  55. }
  56. System.out.println("/");
  57. }
  58. }
  59.  
  60. // This produces the bottom half of the hourglass figure
  61. public static void bottom()
  62. {
  63. for (int j=1; j <=SIZE; j++)
  64. {
  65. int i;
  66. for (i=1; i <=(SIZE+1)-j; i++){
  67. System.out.print(" ");
  68. }
  69. System.out.print("/");
  70. int k;
  71. for (k=1; k<=j; k++){
  72. System.out.print("::");
  73. }
  74. System.out.println("\\");
  75. }
  76. }
  77. // Produces the middle bars
  78. public static void vertical()
  79. {
  80. int i;
  81. for (i=1; i <=SIZE+1; i++)
  82. {
  83. System.out.print(" ");
  84. }
  85. System.out.println("||");
  86. }
  87. }
Success #stdin #stdout 0.12s 53140KB
stdin
import javax.swing.JOptionPane;

public class atividadtablasoscar
{
    public static void main (String[] args)
    {
        String menu, mensaje="";
        int opc=0, factorial, resultado=1, x, tabla; 
        int opcion; 
        do
        {
            try
            {
                menu="\n1) Tabla de Multiplicar \n2) Factorial \n3) Salir"; 
                opc=Integer.parseInt(JOptionPane.showInputDialog("Elige una opcion:"+menu));

                if (opc == 1)
                {
                    do 
                    {
                        mensaje="";
                        try
                        {
                            tabla=Integer.parseInt(JOptionPane.showInputDialog("Indica el numero de la tabla a multiplicar:"));
                            for (x=1; x<=10; x++)
                            {
                                mensaje=mensaje+"\n"+x+"*"+tabla+"="+(x * tabla);
                            }
                            JOptionPane.showMessageDialog(null, "La tabla es:"+mensaje);
                            
                           
                            opcion = JOptionPane.showConfirmDialog(null,"¿Quieres hacer otra tabla?","Confirmación", JOptionPane.YES_NO_OPTION);
                            
                        } catch (Exception e)
                        {
                            JOptionPane.showMessageDialog(null, "El valor no es un numero valido");
                         
                            opcion = JOptionPane.NO_OPTION; 
                        }

                    } while (opcion == JOptionPane.YES_OPTION); 
                }

                if (opc == 2)
                {
                    do
                    {
                        resultado=1;
                        try
                        {
               
                            factorial=Integer.parseInt(JOptionPane.showInputDialog("Indica el numero para calcular su factorial:"));
                            if (factorial < 0) {
                                JOptionPane.showMessageDialog(null, "caracter no valido");
                                opcion = JOptionPane.YES_OPTION;
                                continue;
                            }
                            
                            for (x=1; x<=factorial; x++)
                            {
                                resultado=x*resultado;
                            }
                            JOptionPane.showMessageDialog(null, "El factorial de " + factorial + " es: " + resultado);
                            
                                                       opcion= JOptionPane.showConfirmDialog(null,"¿Deseas calcular otro factorial?","Confirmación", JOptionPane.YES_NO_OPTION);

                        } catch (Exception e)
                        {
                            JOptionPane.showMessageDialog(null, "El valor no es un numero valido");
                                                     opcion = JOptionPane.NO_OPTION; 
                        }

                   
                    } while (opcion== JOptionPane.YES_OPTION);
                }
                
                if (opc < 1) {
                     JOptionPane.showMessageDialog(null, "Opción de menú no válida. Por favor, elige 1, 2 o 3.");
                }

            } catch (Exception e)
            {
                JOptionPane.showMessageDialog(null, "El valor introducido no es un número válido para la opción del menú.");
                opc = 0; 
            }
        } while (opc != 3);
        
       
        JOptionPane.showMessageDialog(null, "ADIOS");
    }
}
stdout
|""""""""""""""""""""""""""""""""""""|
 \::::::::::::::::::::::::::::::::::/
  \::::::::::::::::::::::::::::::::/
   \::::::::::::::::::::::::::::::/
    \::::::::::::::::::::::::::::/
     \::::::::::::::::::::::::::/
      \::::::::::::::::::::::::/
       \::::::::::::::::::::::/
        \::::::::::::::::::::/
         \::::::::::::::::::/
          \::::::::::::::::/
           \::::::::::::::/
            \::::::::::::/
             \::::::::::/
              \::::::::/
               \::::::/
                \::::/
                 \::/
                  ||
                 /::\
                /::::\
               /::::::\
              /::::::::\
             /::::::::::\
            /::::::::::::\
           /::::::::::::::\
          /::::::::::::::::\
         /::::::::::::::::::\
        /::::::::::::::::::::\
       /::::::::::::::::::::::\
      /::::::::::::::::::::::::\
     /::::::::::::::::::::::::::\
    /::::::::::::::::::::::::::::\
   /::::::::::::::::::::::::::::::\
  /::::::::::::::::::::::::::::::::\
 /::::::::::::::::::::::::::::::::::\
|""""""""""""""""""""""""""""""""""""|