Problem calling methods in the menu

-1

Hi, I would like to know if someone can clarify how to call methods in java menus. My question is the following (attached program for the example):

public void imprimir() {
        Nodo reco=raiz;
        System.out.println("Listado de todos los elementos de la pila.");
        while (reco!=null) {
            System.out.print(reco.info+"-");
            reco=reco.sig;
        }
        System.out.println();
    }
    public void vacia() {
        if(raiz==null) {
            System.out.println("La pila esta vacia");
        }
        else {
            System.out.println("Todavia caben mas valores en la pila, sigue insertando");
        }
    }
    public void cantidad(){
        Nodo reco=raiz;
        int contador=0;
        if(reco!=null) {
            contador++;
            reco=reco.sig;
        }
        else {
            System.out.println("Hay "+contador+" nodos.");
        }
    }

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner entrada=new Scanner(System.in);
    System.out.println("1-Insertar  2-Extraer   3-Imprimir");
    int op;
    System.out.println("Declara la opcion que desea tomar :");
    op=entrada.nextInt();
    do {
        switch(op) {
        case 1:int x;System.out.println("Declara el numero a insertar");x=entrada.nextInt();insertar(x);break;
        case 2:extraer;break;
        case 3:imprimir();break;
        }
    }while(op!=1||op!=2||op!=3);

    }

 }

My doubt is in the switch case, when creating the cases I do not understand because sometimes we have to put in parentheses variables and sometimes not, and in some cases I get an error when I put the variable that I think is like in this case. If someone can explain how it works I would appreciate it. Thanks in advance.

    
asked by winnie 29.05.2018 в 13:07
source

1 answer

0

If you have a specific error with your switch it would be good if you shared with the community what it is so that someone can help you solve it.

As for the switches, the structure of a switch in Java:

int number = 0;
switch (number) {
        case 1:  System.out.println("El valor del numero es 1");
                 break;
        case 2:  System.out.println("El valor del numero es 2");
                 break;
        default: System.out.println("El valor del numero no es 1 ni 2.");
                 break;
    }

The switches compare the value of the variable that is between the parentheses (in this case number ) with the different cases that are specified. In case the variable does not coincide with any of the cases, the program would use the default option.

This switch would be the same as:

if (number == 1) {
    System.out.println("El valor del numero es 1.");
}
else if (number == 2) {
    System.out.println("El valor del numero es 2.");
} else {
    System.out.println("El valor del numero no es 1 ni 2.");
}

I do not know if that could make you a little clearer. I also leave the Switch documentation in Java so you can have a look: Switch

To call a method from a switch would be the same as calling from outside of it.

Name of the method (Variables you need);

The methods may need variables or not, depending on how you declared it. Let's use the first method you've shared as an example:

public void imprimir() {
    Nodo reco=raiz;
    System.out.println("Listado de todos los elementos de la pila.");
    while (reco!=null) {
        System.out.print(reco.info+"-");
        reco=reco.sig;
    }
    System.out.println();
}

This method imprimir() does not need variables, if for example imprimir(int numero) you would have to pass a variable of type int when calling the method, in this simply call the method would be sufficient, for example:

case 1:
    imprimir();
    break;

For the case imprimir(int numero) would be something kind:

case 1:
  int variable_numero = 0;
  imprimir(variable_numero);
  break;
    
answered by 29.05.2018 в 13:20