My program does not ask for the second Java value

2

I need help with my code in java, I'm doing a menu with functional options but with a graphical interface, when I click on the options, for example, adding, I'm asking for the first value but the second one does not ask for it and the program arrives there closes alone, the idea would be to ask for the second value and show the result on the screen, the other thing is that in other options it does not show the data in the menu but in the console. If someone had the time and knowledge to review the code and correct it and if you could explain a little bit about what the error was, I would really appreciate it if you need help.

Here I leave the download link of the code is in a document .txt link

This is the code:

package jesus.menu;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import java.util.*;
import java.awt.Toolkit;

public class JesusMenu {
static double numero;
static Scanner scanner = new Scanner(System.in); //Sirve para recoger texto por consola
static int select = -1; //Es la opción elegida por el usuario
static double num1 = 0, num2 = 0; //Son las variables

public static void main(String[] args) {

    Scanner teclado=new Scanner(System.in);//Esto es para que se lean datos desde el teclado.
Calendar fecha = new GregorianCalendar();   //Se agrega el objeto calendario
//Se declaran las variables para la fecha y hora
int ano = fecha.get(Calendar.YEAR);
    int mes = fecha.get(Calendar.MONTH);
    int dia = fecha.get(Calendar.DAY_OF_MONTH);
    int hora = fecha.get(Calendar.HOUR_OF_DAY);
    int minuto = fecha.get(Calendar.MINUTE);
    int segundo = fecha.get(Calendar.SECOND);

    String strOpcion;
    int opcion = 0;

    do{
        strOpcion = JOptionPane.showInputDialog("** ELIJA UNA OPCION DEL MENU **:\n\n" +
                                                "1.- Sumar\n" +
                                                "2.- Restar\n" +
                                                "3.- Multiplicar\n" +
                                                "4.- Dividir\n" +
                                                "5.- Raiz de un numero\n" +
                                                "6.- Mostrar Fecha\n" +
                                                "7.- Mostar Hora\n" +
                                                "8.- Genera un numero aleatorio\n" +
                                                "9.- Resolucion\n" +
                                                "0.- Salir\n");
    try{
        opcion = Integer.parseInt(strOpcion);






    }catch(NumberFormatException ex){
        JOptionPane.showMessageDialog(null,"***Hubo un Error***");
    }
    switch(opcion){
    case 1: 
            JOptionPane.showInputDialog("Introduce un numero para sumarlo");
    pideNumeros();
    JOptionPane.showInputDialog(num1+" + "+num2+" = "+(num1+num2));
            break;
case 2:
    JOptionPane.showInputDialog("Introduce un numero para restarlo");
    pideNumeros();
    JOptionPane.showInputDialog(num1+" - "+num2+" = "+(num1-num2));
    break;
case 3: 
    JOptionPane.showInputDialog("Introduce un numero para multiplicarlo");
    pideNumeros();
    JOptionPane.showInputDialog(num1+" * "+num2+" = "+(num1*num2));
            break;
case 4: 
    JOptionPane.showInputDialog("Introduce un numero para dividirlo");
    pideNumeros();
    JOptionPane.showInputDialog(num1+" / "+num2+" = "+(num1/num2));
    break;
case 5:
    JOptionPane.showInputDialog("Introduce el Numero para Obtener su Raiz");
    double numeros=teclado.nextInt();
    double resultado=Math.sqrt(numeros);
    JOptionPane.showInputDialog("El resultado es: "+resultado);
    JOptionPane.showInputDialog("\n");
    break;
case 6:
    JOptionPane.showInputDialog("nFecha Actual: " + dia + "/" + (mes+1) + "/" + ano);
    break;
case 7:
    System.out.printf("Hora Actual: %02d:%02d:%02d %n", hora, minuto, segundo);
    break;
case 8:
            JOptionPane.showInputDialog("El numero generado es: ");
    for (int i=0; i<1; i++){
    numeros=Math.random()*50;
    System.out.println(numeros);
    System.out.println("");
    }
    break;
case 9:
    JOptionPane.showInputDialog("Se mostrara la resolucion de la pantalla");
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();//Se obtiene el tamaño de la pantalla
    int sr = Toolkit.getDefaultToolkit().getScreenResolution();//Se obtiene la resolucion de la pantalla
    JOptionPane.showInputDialog("Tamaño de pantalla: " + d.width + "x" + d.height);
    break;
case 0: 
    JOptionPane.showInputDialog("\nHasta Luego...\n");
    break;
default: JOptionPane.showMessageDialog(null,"Opcion no valida");break;

    }

    }while(opcion!=0);
}

private static void pideNumeros() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

I would appreciate it too much. THANK YOU.

    
asked by Jesus RC 05.06.2016 в 05:15
source

1 answer

4

I have tested your code and launches a exception because your method pideNumeros() is released%% of%

I leave the code to make the sum, that is, option 1 of your program.

import java.awt.Dimension;
import javax.swing.JOptionPane;
import java.util.*;
import java.awt.Toolkit;

public class JesusMenu {

    static double numero;
    static Scanner scanner = new Scanner(System.in); //Sirve para recoger texto por consola
    static int select = -1; //Es la opción elegida por el usuario
    static double num1 = 0, num2 = 0; //Son las variables

    public static void main(String[] args) {

        Scanner teclado=new Scanner(System.in);//Esto es para que se lean datos desde el teclado.
    Calendar fecha = new GregorianCalendar();   //Se agrega el objeto calendario
    //Se declaran las variables para la fecha y hora
    int ano = fecha.get(Calendar.YEAR);
        int mes = fecha.get(Calendar.MONTH);
        int dia = fecha.get(Calendar.DAY_OF_MONTH);
        int hora = fecha.get(Calendar.HOUR_OF_DAY);
        int minuto = fecha.get(Calendar.MINUTE);
        int segundo = fecha.get(Calendar.SECOND);

        String strOpcion;
        int opcion = 0;

        do{
            strOpcion = JOptionPane.showInputDialog("** ELIJA UNA OPCION DEL MENU **:\n\n" +
                                                    "1.- Sumar\n" +
                                                    "2.- Restar\n" +
                                                    "3.- Multiplicar\n" +
                                                    "4.- Dividir\n" +
                                                    "5.- Raiz de un numero\n" +
                                                    "6.- Mostrar Fecha\n" +
                                                    "7.- Mostar Hora\n" +
                                                    "8.- Genera un numero aleatorio\n" +
                                                    "9.- Resolucion\n" +
                                                    "0.- Salir\n");
        try{
            opcion = Integer.parseInt(strOpcion);
        }catch(NumberFormatException ex){
            JOptionPane.showMessageDialog(null,"***Hubo un Error***");
        }
        switch(opcion){
        case 1: 
                num1 = Double.parseDouble(JOptionPane.showInputDialog("Introduce el primer numero"));
                num2 = Double.parseDouble(JOptionPane.showInputDialog("Introduce el segundo numero"));
                //pideNumeros(); //Comento el método para no lanzar un exception
                JOptionPane.showMessageDialog(null,num1+" + "+num2+" = "+(num1+num2));
            break;
    case 2:
        JOptionPane.showInputDialog("Introduce un numero para restarlo");
        pideNumeros();
        JOptionPane.showInputDialog(num1+" - "+num2+" = "+(num1-num2));
        break;
    case 3: 
        JOptionPane.showInputDialog("Introduce un numero para multiplicarlo");
        pideNumeros();
        JOptionPane.showInputDialog(num1+" * "+num2+" = "+(num1*num2));
                break;
    case 4: 
        JOptionPane.showInputDialog("Introduce un numero para dividirlo");
        pideNumeros();
        JOptionPane.showInputDialog(num1+" / "+num2+" = "+(num1/num2));
        break;
    case 5:
        JOptionPane.showInputDialog("Introduce el Numero para Obtener su Raiz");
        double numeros=teclado.nextInt();
        double resultado=Math.sqrt(numeros);
        JOptionPane.showInputDialog("El resultado es: "+resultado);
        JOptionPane.showInputDialog("\n");
        break;
    case 6:
        JOptionPane.showInputDialog("nFecha Actual: " + dia + "/" + (mes+1) + "/" + ano);
        break;
    case 7:
        System.out.printf("Hora Actual: %02d:%02d:%02d %n", hora, minuto, segundo);
        break;
    case 8:
                JOptionPane.showInputDialog("El numero generado es: ");
        for (int i=0; i<1; i++){
        numeros=Math.random()*50;
        System.out.println(numeros);
        System.out.println("");
        }
        break;
    case 9:
        JOptionPane.showInputDialog("Se mostrara la resolucion de la pantalla");
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();//Se obtiene el tamaño de la pantalla
        int sr = Toolkit.getDefaultToolkit().getScreenResolution();//Se obtiene la resolucion de la pantalla
        JOptionPane.showInputDialog("Tamaño de pantalla: " + d.width + "x" + d.height);
        break;
    case 0: 
        JOptionPane.showMessageDialog(null,"\nHasta Luego...\n");
        break;
    default: JOptionPane.showMessageDialog(null,"Opcion no valida");break;

        }

        }while(opcion!=0);
    }

    private static void pideNumeros() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

If you notice in the switch case 1 I have commented on the call to the method asksNumbers (), and instead I am calling the data entry of the two numbers through throw new UnsupportedOperationException("Not supported yet."); .

EDIT based on comments

To perform the operations you mention in the comments, you could do something like this:

      case 5:
           num1 = Double.parseDouble(JOptionPane.showInputDialog("Introduce el primer numero"));        
           double resultado=Math.sqrt(num1);
           JOptionPane.showMessageDialog(null,"El resultado es: "+resultado);

         break;
    case 6:
        JOptionPane.showInputDialog("Fecha Actual: " + dia + "/" + (mes+1) + "/" + ano);
        break;
    case 7:
                JOptionPane.showMessageDialog(null,"Hora Actual: "+ hora + ":"+minuto+":"+segundo);
        //System.out.printf("Hora Actual: %02d:%02d:%02d %n", hora, minuto, segundo);
        break;
    case 8:
                double numeros=Math.random()*50;
                JOptionPane.showMessageDialog(null, "El numero generado es: "+numeros);
        break;
    
answered by 05.06.2016 / 06:17
source