Problem with Java switch

1

I have a menu with a switch with 3 options that does the following:

  • Data Capture
  • Data query
  • Finish
  • In option 1 the user is requested all the necessary data. In option 2 I want to show this data that was saved.

    The question is, how can I do this? How can I show the data that was saved in the variables in option 1?.

    Code:

    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    public class Menu_1 extends Variables {
       public static void main(String[]args) {
      int opcion;
        do{
            opcion=Integer.parseInt(JOptionPane.showInputDialog(null,
    
                    "   ELIJA LA OPCION QUE NECESITA UTILIZAR  \n"+
    
                    "1.- Captura de datos \n"+
    
                    "2.- Consulta de datos \n"+
    
                    "3.- Finalizar \n"+
    
                    "INGRESE UNA OPCION(1-3)","MENU PRINCUPAL",JOptionPane.QUESTION_MESSAGE)); 
           switch(opcion){
            case 1: 
       int identificacion;  
        short edad;
        long telefono; 
        int sueldo;
        String direccion; 
        float estatura; 
        double deuda; 
        boolean datos;
        char genero; 
        String nombre;  
        String estudios;
        Short familiares; 
           Scanner teclado= new Scanner (System.in);
           System.out.print("Diguite la direccion: ");
           direccion= teclado.nextLine();  
    
           System.out.print("Diguite la identificacion: ");
           identificacion= teclado.nextInt();  
    
           System.out.print("Diguite el nombre: ");
           teclado.nextLine(); 
           nombre= teclado.nextLine(); 
    
           System.out.print("Diguite la edad: ");
           edad= teclado.nextShort();  
    
           System.out.print("Diguite el telefono: ");
           telefono= teclado.nextLong();  
    
           System.out.print("Diguite el sueldo: ");
           sueldo= teclado.nextInt(); 
    
           System.out.print("Diguite la estatura: ");
           estatura= teclado.nextFloat(); 
    
           System.out.print("Diguite la deuda: ");
           deuda= teclado.nextDouble(); 
    
           System.out.print("Diguite los estudios: ");
           teclado.nextLine();
           estudios= teclado.nextLine();  
    
           System.out.print("Diguite la cantidad de familiares: ");
           familiares= teclado.nextShort(); 
    
           System.out.print("digite su genero (Masculino o femenino):");
           genero= teclado.next().charAt(0);break;
                case 2: 
              //aqui va la opcion 2 mostrar contenido que fue capturado en la opcion 1, aquí el problema o de que otra manera hacer el menu funcional//
    
                case 3: JOptionPane.showMessageDialog(null,"QUE TENGA BUEN DIA");System.exit(0);break;  
                    default:JOptionPane.showMessageDialog(null, "ELIJA UNA OPCION VALIDA \n", "ERROR DE OPCION",JOptionPane.WARNING_MESSAGE);
        } }while(opcion !=3);
        }    
        }
    
        
    asked by Juan Andres Molano 04.03.2018 в 23:40
    source

    1 answer

    2

    I have understood your question about your exercise, although I have struggled to achieve it because of your poor explanation of the problem.

  • Variables must be moved at the beginning of your entire program so that they can be accessed from any part of the switch.

  • I have changed your variables of type short , Long to type int since the latter is the easiest to work with. In the same way I have changed float to double .

  • Fixed code:

    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    public class Menu_1 {
        public static void main(String[] args) {
    
            // Variables
            // Podrán ser accedidas desde cualquier parte del switch
            // Nota: se inicializan las variables
            int identificacion = 0;
            int edad = 0; // He cambiado de short a int
            int telefono = 0; // He cambiado de Long a int
            int sueldo = 0;
            String direccion = null;
            double estatura = 0; // He cambiado de float a double
            double deuda = 0;
            char genero = 0;
            String nombre = null;
            String estudios = null;
            int familiares = 0; // He cambiado de short a int
            int opcion;
    
            do {
                opcion = Integer.parseInt(JOptionPane.showInputDialog(null,
    
                        "   ELIJA LA OPCION QUE NECESITA UTILIZAR  \n" +
    
                                "1.- Captura de datos \n" +
    
                                "2.- Consulta de datos \n" +
    
                                "3.- Finalizar \n" +
    
                                "INGRESE UNA OPCION(1-3)",
                        "MENU PRINCUPAL", JOptionPane.QUESTION_MESSAGE));
                switch (opcion) {
                case 1:
                    Scanner teclado = new Scanner(System.in);
                    System.out.print("Diguite la direccion: ");
                    direccion = teclado.nextLine();
    
                    System.out.print("Diguite la identificacion: ");
                    identificacion = teclado.nextInt();
    
                    System.out.print("Diguite el nombre: ");
                    teclado.nextLine();
                    nombre = teclado.nextLine();
    
                    System.out.print("Diguite la edad: ");
                    edad = teclado.nextInt();
    
                    System.out.print("Diguite el telefono: ");
                    telefono = teclado.nextInt();
    
                    System.out.print("Diguite el sueldo: ");
                    sueldo = teclado.nextInt();
    
                    System.out.print("Diguite la estatura: ");
                    estatura = teclado.nextDouble();
    
                    System.out.print("Diguite la deuda: ");
                    deuda = teclado.nextDouble();
    
                    System.out.print("Diguite los estudios: ");
                    teclado.nextLine();
                    estudios = teclado.nextLine();
    
                    System.out.print("Diguite la cantidad de familiares: ");
                    familiares = teclado.nextInt();
    
                    System.out.print("digite su genero (Masculino o femenino):");
                    genero = teclado.next().charAt(0);
    
                    // Cerrar el Scanner
                    teclado.close();
                    break;
                case 2:
                    // Masculino
                    if (genero == 'm' || genero == 'M') {
                        System.out.println("\nSeñor " + nombre + ",\ncon identificación No: " + identificacion + ",\nsu información extraída de la base de datos es la siguiente:");
                        System.out.println("Tiene " + edad + " años de edad,\nvive en la " + direccion + ",\nsu teléfono es " + telefono + ",\nmide " + estatura + " metros de estatura"
                                + ",\nsu sueldo es de " + sueldo + ", \nsu deuda es de " + deuda + ".\nUsted es " + estudios + " y tiene " + familiares + " familiares.");
                    } else { // Femenino
                        System.out.println("\nSeñora " + nombre + ",\ncon identificación No: " + identificacion + ",\nsu información extraída de la base de datos es la siguiente:");
                        System.out.println("Tiene " + edad + " años de edad,\nvive en la " + direccion + ",\nsu teléfono es " + telefono + ",\nmide " + estatura + " metros de estatura"
                                + ",\nsu sueldo es de " + sueldo + ", \nsu deuda es de " + deuda + ".\nUsted es " + estudios + " y tiene " + familiares + " familiares.");
                    }
           break;
    
                case 3:
                    JOptionPane.showMessageDialog(null, "QUE TENGA BUEN DIA");
                    System.exit(0);
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "ELIJA UNA OPCION VALIDA \n", "ERROR DE OPCION",
                            JOptionPane.WARNING_MESSAGE);
                }
            } while (opcion != 3);
        }
    }
    

        
    answered by 05.03.2018 / 00:15
    source