Is it possible to make a case switch by entering a value of type char?

1
 public static void main(String[] args) {
        World hoja = new World();
        Turtle tortu = new Turtle(hoja);
        Scanner lector = new Scanner(System.in);
        String opcion = "";
        char opc;
         do{                   
             do{
         System.out.println("Ingrese el tipo de dibujo que desea realizar:"
                 + "\nRed String ........ a \n Cuadrado ..... b \nTriangulo ..... c"
                 + "\nCuadrilatero ....... d \n Círculo ... e \nSalir.....f");
            opcion = lector.next();
            if(!opcion.matches("[a-f]?")){    
                System.out.println("Error, Opcion no valida o en mayusculas. \n");
               } 
           }while(!opcion.matches("[a-fA-F]?")); 
             opc = opcion.charAt(0);
             String a = "";
             String b = "";
             String c = "";
             String d = "";     

             Switch(opc){
             case a:
             System.out.println("Ingrese la coordenada x de la Red Sting:");
             do{
                a = lector.next();
                if(){

                }
            }while();

            }

        }while(opcion!=f );
    }
}
    
asked by Salvador Garcia 04.04.2018 в 07:55
source

1 answer

1

Here I leave a simple example for you to see how it is implemented. And make sure to close each "case" that you have with a "break" or it will give you many errors and then you will not know where to close them exactly.

public static void main(String[] args) {
        Scanner out = new Scanner(System.in);
        char opc;

        System.out.println("Dime una letra de la a a la c: ");
        opc = out.nextLine().charAt(0);

        switch(opc){
            case 'a':
                System.out.println("La letra es la a.");
                break;
            case 'b':
                System.out.println("La letra es la b.");
                break;
            case 'c':
                System.out.println("La letra es la c.");
                break;
        }

    }
    
answered by 04.04.2018 в 08:04